Routing - Area Controller/View with parameter

2019-08-05 11:23发布

问题:

Super simple MVC site with an Area to handle mobile devices. All of my Area routing works fine with the exception of a view that expects a parameter.

In the "normal" site I have a view video page that expects a parameter.

mysite.com/Video/123456

This works perfectly. After fighting this for a bit in my Area for the mobile content I have even gone down to using the exact same code/markup in my Controller and View. So I would expect that the following URL:

mysite.com/Mobile/Video/123456

Would resolve properly. It doesn't. I get a 404 (not found). If I take the parameter off:

mysite.com/Mobile/Video

It resolves properly.

I am sure this must be something I am doing wrong in the routing. Below is the appropriate section from my global.asax. Any help would be appreciated.

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

        routes.MapRoute( 
            "Video", // Route name 
            "Video/{id}", // URL with parameters 
            new { controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
            new string[] { "mysite.Controllers.VideoController" } 
        ); 

        routes.MapRoute( 
            "NewsItem", // Route name 
            "NewsItem/{id}", // URL with parameters 
            new { controller = "NewsItem", 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 
            new string[] { "mysite.Controllers.HomeController" } 
        ); 

        routes.MapRoute( 
            "Mobile", // Route name 
            "{controller}/{action}/{id}", // URL with parameters 
            new { area = "Mobile", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
            new string[] { "mysite.Areas.Mobile.Controllers.HomeController" } 
        ); 

        routes.MapRoute( 
            "Mobile/Video", // Route name 
            "Mobile/Video/{id}", // URL with parameters 
            new { area = "Mobile", controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
            new string[] { "mysite.Areas.Mobile.Controllers.VideoController" } 
        ); 
    }

回答1:

SteveInTN, you cannot have the same registration in both, Global.asax and MobileAreaRegistration.cs.

You only need to have Mobile Registration on MobileAreaRegistration.cs and call AreaRegistration.RegisterAllAreas() in Application_Start before RegisterRoutes(RouteTable.Routes).

If you want url like mysite.com/Mobile/Video/123456: The mobile route registration should be in the format {controller} / {id}, like video route.

Registration in Global.asax:

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

    routes.MapRoute( 
        "Video", // Route name 
        "Video/{id}", // URL with parameters 
        new { controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
        new string[] { "mysite.Controllers.VideoController" } 
    ); 
    //newsitem route
}

Registration on MobileAreaRegistration:

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


回答2:

Looks like your Route name should not contain / since it may conflict with routing? When I do routing I make sure the names are unique and use underscores to represent separators like so : text_text. Not sure if this will work, worth a try though.