Asp.net MVC Catchall Routing in a Sub Application

天涯浪子 提交于 2020-01-03 06:59:07

问题


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()));

回答1:


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




回答2:


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;
});


来源:https://stackoverflow.com/questions/39328039/asp-net-mvc-catchall-routing-in-a-sub-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!