【问题标题】:Routing - Area Controller/View with parameter路由 - 带参数的区域控制器/视图
【发布时间】:2011-11-03 19:46:58
【问题描述】:

超级简单的 MVC 站点,带有一个处理移动设备的区域。除了需要参数的视图之外,我所有的区域路由都可以正常工作。

在“正常”站点中,我有一个需要参数的观看视频页面。

mysite.com/Video/123456

这非常有效。在我的区域为移动内容进行了一些斗争之后,我什至开始在我的控制器和视图中使用完全相同的代码/标记。所以我希望以下网址:

mysite.com/Mobile/Video/123456

会正确解决。它没有。我得到一个 404(未找到)。如果我关闭参数:

mysite.com/Mobile/Video

正确解析。

我确定这一定是我在路由中做错了什么。以下是我的 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" } 
        ); 

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

【问题讨论】:

  • 您正在尝试在 global.asax 中注册区域?
  • 是的,上面的代码 sn-p 来自我的 global.asax。我还在我的 MobileAreaRegistration.cs 文件中注册了一条路线。
  • context.MapRoute("Mobile_default", "Mobile/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } );跨度>

标签: asp.net-mvc-3 asp.net-mvc-3-areas


【解决方案1】:

SteveInTN,您不能在 Global.asax 和 MobileAreaRegistration.cs 中进行相同的注册。

您只需要在 MobileAreaRegistration.cs 上进行移动注册,并在 RegisterRoutes(RouteTable.Routes) 之前在 Application_Start 中调用 AreaRegistration.RegisterAllAreas()。

如果您想要类似 mysite.com/Mobile/Video/123456 的网址: 移动路由注册格式应为{controller} / {id},如视频路由。

在 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
}

在 MobileAreaRegistration 上注册:

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

【讨论】:

  • 好的,我在两者中没有相同的注册,并确认我在 global.asax 中调用了 RegisterAllAreas()。我从 global.asax 中删除了我的 Mobile AREA 中所有已注册的路线。我的所有路线仍然运行良好,除非我尝试将 ID 传递到移动区域中的视频页面。比如路由mobile/video/23079(编号为ID)返回“resource not found”,但mobile/video解析正常。
  • 如果您想导航到 mysite.com/mobile/videos/13,请从移动路线中删除 {action}。移动路线必须是 {controller}/{id},就像您的视频路线一样。
  • Stackoverlow 太棒了!!同样的问题在“其他”网站上询问 MVC 3 问题好几天了。应该先来这里。感谢大家的帮助!
【解决方案2】:

看起来您的路由名称不应该包含 / 因为它可能与路由冲突?当我进行路由时,我确保名称是唯一的,并使用下划线来表示分隔符,如下所示:text_text。不确定这是否可行,但值得一试。

【讨论】:

  • Lucas,我​​从移动/视频路由名称中删除了 / 并将其更改为 Mobile_Video。仍然没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多