Routing in MVC application deployed on IIS - c#

I have added the following code
public class RouteController : Controller
{
public ContentResult GetImpression()
{
// Do something
}
}
In RouteConfig class i have added the following
routes.MapRoute(
name: "Impression",
url: "imp",
defaults: new { controller = "Route", action = "GetImpression", id = UrlParameter.Optional }
);
I am expecting my http://mymachine/imp to work. What am i doing wrong? Do i have to do some settings in IIS as well?

For issues with routing I have always found the following tool from Phil Hacck to be invaluable in figuring out where I screwed up with my routing rules and what is getting called. There is nothing special you need to do in IIS to get things working. The one thing you probably should check is to ensure if your Application Pool is using version 4.0 and not version 2.0. Usually when you create a site in IIS it defaults the App Pool to 2.0.

Related

Asp.net MVC Catchall Routing in a Sub Application

I have an MVC application with a sub application running another MVC project in IIS. Both use the same version framework and run on separate application pools.
My problem is, I cannot get the sub application to work inside this virtual application folder of the root site. I get a 403.14 Forbidden error. If I enable directory listing on the sub application I just get a list of the MVC application files.
I think, I have narrowed the problem down to routing; The sub application has a custom catchall route which handles all requests to the site, its a CMS application. There are no other routes registered. Here is the code for my custom route:
RouteTable.Routes.Insert(0,new CmsRoute(
"{*path}",
new RouteValueDictionary(new
{
controller = "Page",
action = "Index"
}),
new MvcRouteHandler()));
Running this application outside of the main root application in its own site, it works fine and all requests are handled by my custom route. But as soon as I try run this as a sub application the route handler is never hit.
If I add the standard default MVC route that comes out of the box:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then MVC kicks in and tries to find a match route for the request.
So my question is why will my catchall route not work while running in a sub application in IIS ?
Note: I have tried prefixing the url in my custom route with the name of the virtual application folder, but it still does not work, example:
RouteTable.Routes.Insert(0,new CmsRoute(
"subapplication/{*path}",
new RouteValueDictionary(new
{
controller = "Page",
action = "Index"
}),
new MvcRouteHandler()));
Use sub domain rather than sub application if that doesn't hurt your purpose.
But, if you are bound to do that,remember this the application and sub application have to be running on the same version of .NET.
In addition, there is a parent/child relationship with the web.configs - so the child application will inherit the parents web.config unless told not to do so using 'inheritInChildApplications' in the configs. But, that might also give you headache of clashing with child. here is a solution to this(headache) if you want to try parent/child relationship . http://articles.runtings.co.uk/2010/04/solved-iis7-validateintegratedmodeconfi.html
When you send a request from a client to an application, it doesn't get transferred to another application (like your sub-application) unless you specifically make the call. That's why your sub-application works fine when you access it directly but fails when you try to reach it through the parent the way you're doing it. Routing is about requests and it's the application that got the request that is responsible for the routing issues.
So what you need to do is make an actual redirect to your sub-application. Your sub-application should have a separate URL (a sub-domain will be fine). The code should look like this:
public ActionResult YourAction() //Action in the main application
{
var subAppUrl = "http://yourSubApplicationUrl?AnyOtherQueryStringSetToPassData";
return Redirect(subAppUrl);
}
If you're calling the action using Ajax, then it should be:
public JsonResult YourAction() //Action in the main application
{
var subAppUrl = "http://yourSubApplicationUrl?AnyOtherQueryStringSetToPassData";
return Json(new {url = subAppUrl });
}
and the corresponding JQuery:
$.post("#Url.Action("YourAction")", function(data) {
window.location = data.url;
});
Your SubApplication it's already the Path:
RootApp /
SubApp1 /SubApp1
SubApp2 /SubApp2
routes.MapRoute(
"SubApp1", // Route name
"{*url}", // URL with parameters
new
{
controller = "Controller",
action = "Action",
id = UrlParameter.Optional
}
);
And.. you should remove on the root app the MapRoute to your SubApp1 if already managed!

How to handle a virtual path created by proxy server in MVC route?

I am fairly new to MVC. Recently I developed a site and hosted it behind a proxy server. I access my site using internalhostname/site/{controller}/{action}/{id} for test, which works fine.
However, when my users are connecting the site, they would use an external url like this: externalhostname/apps/site/{controller}/{action}/{id}. Now when my views attempt to initiate a call to the controller according to the default route config, the URLs being generated become externalhostname/site/{controller}/{action}/{id}, notice "/apps" is gone.
Well, this is a known problem to me. Because when creating the URL, host name does not include "/apps". In other sites created in regular ASP.NET page, I would simply hijack the URL creation and replace host with host/apps, that can fix the issue. But I don't know how to do this in MVC world.
Also I am using Telerik controls (for MVC) that also initiate requests by controller and action, which lead to the wrong URL eventually.
The route config is default. I did try to change the rule but all of those only affect the url format after . Nothing could allow me to change the behavior ahead of it.
I have been struggling for days and couldn't see a way out. Appreciate for some advice. Thank you.
I cannot change the proxy rule. That is not an option to me.
Environment: MVC 4, IIS 7.0, if these matter.
Try to add new entry in your route config.
routes.MapRoute(
"Default2", // Route name
"apps/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults,
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Take note of the order.

HTTP Error 404.0 - Not Found in Mvc 6 Asp.net 5

I'm using mvc beta6 and i want to have dynamic controller.
Right now i have created only two controller Home and Dynamic.
so default routing is controller/action and which was running fine till mvc 5
even we enter xyz/someaction in browser url.
But in case of mvc 6 it won't fire defaultcontroller factory and gives http error 404.because of that im not able to route to dynamic controller as user type in browser.
this is my start up code..
public void Configure(IApplicationBuilder app)
{
//app.UseMvc();
//// app.UseMvcWithDefaultRoute();
app.UseMvc(routes =>
{
/// routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");
routes.MapRoute("defaultRoute",
"{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
}
I think routing is only registered for actual controller existed in app during compilation time.if i'm wrong then please tell me about the dynamic routing.
I've got the solution for my problem.The way of finding controller in mvc6 is very different from its previous versions.In mvc 6 all the controller types are discovered during compile time and cached through the DI framework and routing are registered to only those cached controller.So system doesnot invoke any factory methods unless it finds the requested controller. :)
That's it!
ps:thanks for everyone who really gave their valuable times.
i think you should try in following manner
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
You can also go with articles on my blog with "Routing in MVC"

MVC.ASP.NET and problem with route

Now I have to fix some bugs in MVC.Asp.Net project. In projec i have route:
Global.asax
routes.MapRoute(
"Category no name",
"_C{id}.cfm",
new { controller = "Products", action = "Category" },
new { id = #"\d+" }
);
Controller:
public ActionResult Category(int id)
{
// Break point here
// doing smth
return View();
}
In view I have link:
#category.Name
Everything is working correct when I use Visual Studio Development Server but when I try to run project on Local IIS server ( I use http://localhost/TestSite) I have problem with this link. It doesn't work and I can't fall to break point in my controller.
Why do I have this behaviour? May be smth wrong with my link? I can't change route in global.asax.
Thanks, for any ideas
I suggest that the IIS is not configured properly. Try to set integrated mode for the IIS pool. Or map .cfm extension to ASP .NET.

ASP.NET MVC in a virtual directory

I have the following in my Global.asax.cs
routes.MapRoute(
"Arrival",
"{partnerID}",
new { controller = "Search", action = "Index", partnerID="1000" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
My SearchController looks like this
public class SearchController : Controller
{
// Display search results
public ActionResult Index(int partnerID)
{
ViewData["partnerID"] = partnerID;
return View();
}
}
and Index.aspx simply shows ViewData["partnerID"] at the moment.
I have a virtual directory set up in IIS on Windows XP called Test.
If I point my browser at http://localhost/Test/ then I get 1000 displayed as expected. However, if I try http://localhost/Test/1000 I get a page not found error. Any ideas?
Are there any special considerations for running MVC in a virtual directory?
IIS 5.1 interprets your url such that its looking for a folder named 1000 under the folder named Test. Why is that so?
This happens because IIS 6 only
invokes ASP.NET when it sees a
“filename extension” in the URL that’s
mapped to aspnet_isapi.dll (which is a
C/C++ ISAPI filter responsible for
invoking ASP.NET). Since routing is a
.NET IHttpModule called
UrlRoutingModule, it doesn’t get
invoked unless ASP.NET itself gets
invoked, which only happens when
aspnet_isapi.dll gets invoked, which
only happens when there’s a .aspx in
the URL. So, no .aspx, no
UrlRoutingModule, hence the 404.
Easiest solution is:
If you don’t mind having .aspx in your
URLs, just go through your routing
config, adding .aspx before a
forward-slash in each pattern. For
example, use
{controller}.aspx/{action}/{id} or
myapp.aspx/{controller}/{action}/{id}.
Don’t put .aspx inside the
curly-bracket parameter names, or into
the ‘default’ values, because it isn’t
really part of the controller name -
it’s just in the URL to satisfy IIS.
Source: http://blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/
If you are doing this on Windows XP, then you're using IIS 5.1. You need to get ASP.Net to handle your request. You need to either add an extension to your routes ({controller}.mvc/{action}/{id}) and map that extension to ASP.Net or map all requests to ASP.Net. The http://localhost/Test works because it goes to Default.aspx which is handled specially in MVC projects.
Additionally, you need to specify http://localhost/Test/Search/Index/1000. The controller and action pieces are not optional if you want to specify an ID.
There are a number of considerations when using virtual directories in your application.
One is particular is that most browsers will not submit cookies that came from one virtual directory to another, even if the apps reside on the same server.
Try set virtual path: right click on mvc project, properties, web tab, there enter appropriate location.

Categories

Resources