Routing - Area Controller/View with parameter

点点圈 提交于 2019-12-24 11:09:02

问题


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.



来源:https://stackoverflow.com/questions/8000933/routing-area-controller-view-with-parameter

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