Routes in areas not working in MVC / Web forms hybrid app

心不动则不痛 提交于 2019-12-11 10:28:13

问题


The app has four parts to it and 2 of them are in Areas like so (the solution is just one project fyi):

/Areas/ProjectA/...
/Areas/ProjectB/...

The models, views, and controllers for each are nested in there. The app with loclhost works fine and all the routes behave normally.

The routes do not work on the IIS server since the URL is like:

https://companywebsite.com/subfolder/

The subfolder for the project is messing with the routes.

The Global.asax file is like so:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if (Request.Url.ToString().ToLower().Contains("projecta/default.aspx"))
        {
            Context.Response.StatusCode = 301;
            Context.Response.Redirect("~/ProjectA/Home/Index");
        }

        if (Request.Url.ToString().ToLower().Contains("projectb/home.aspx"))
        {
            Context.Response.StatusCode = 301;
            Context.Response.Redirect("~/ProjectB/Home/Index");
        }
    }

Teh RouteConfig.cs file is like so:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.RouteExistingFiles = false;
        routes.IgnoreRoute("projectd/{*pathInfo}");

        // Register[Route] attributes
        routes.MapMvcAttributeRoutes();

        routes.MapPageRoute(
            "WebFormDefault",
            string.Empty,
            "~/index.aspx");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

    }

This is the area registration for Project B:

public class ProjectBAreaRegistration : AreaRegistration 
{
    public override string AreaName 
    {
        get 
        {
            return "ProjectB";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            "ProjectB_home",
            "ProjectB/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional });
    }
}

Issues:

  1. https://loclahost:0123/ProjectA routes correctly but https://companywebsite.com/subfolder/ProjectA routes to https://companywebsite.com/ProjectA/Home/Index with a 404 error.
  2. In ProjectB clicking on a link in htts://companywebsite.com/subfolder/ProjectB/Home/home/error?msg=Not%20Found instead of making an ajax call.

来源:https://stackoverflow.com/questions/49928341/routes-in-areas-not-working-in-mvc-web-forms-hybrid-app

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