【问题标题】:ASP.NET MVC 2 Areas 404ASP.NET MVC 2 区域 404
【发布时间】:2010-06-10 04:11:25
【问题描述】:

有没有人能够让 ASP.NET MVC 2 中的区域工作?

我创建了一个名为“Secure”的新区域,并在其中放置了一个名为 HomeController 的新控制器。然后我创建了一个新的 Home/Index.aspx 视图。但是,当我浏览到 http://localhost/myapp/Secure/ 时,它给出了 404 资源找不到。 http://localhost/myapp/Secure/Home 给出同样的错误。

我的区域注册是这样的:

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

我也试过这个:

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

谢谢, 贾斯汀

【问题讨论】:

    标签: asp.net asp.net-mvc-2


    【解决方案1】:

    我很确定这是因为您的区域注册类与它所在的项目位于不同的命名空间中。这将解释为什么您的解决方案有效 - 您使用命名空间中的重载注册它。我有一个类似的问题,并通过更正命名空间来解决。

    【讨论】:

    • 命名空间对我来说是错误的。我错过了“.Areas”。部分
    【解决方案2】:

    当然,我有使用 ASP.NET MVC 2 的区域。

    人们很难通过 SO 调试路由,最简单的方法是使用Phil Haacks Route Debugger。它会告诉您正在为特定 URL 解析哪些路由(和区域)。非常方便。

    不管有什么问题,试着把你的路线改成这样:

           context.MapRoute(
                "Secure_default",
                "Secure",
                new { action = "Index", controller = "Home", id = UrlParameter.Optional }
            );
    

    url (<yourhost>/Secure) 会找到你的区域Secure,但不知道你将请求交给哪个控制器,因为你没有在你的区域路由中指定controller 的默认值。

    这是我的设置:

    Areas
          Albums
               Controllers
                    AlbumsController.cs (namespace Web.Areas.Albums.Controllers)
               Models
               Views
                    Albums
                         Index.aspxx
               AlbumsAreaRegistration.cs
                    context.MapRoute(
                        "Albums_Default",
                        "Albums",
                        new { controller = "Albums", action = "Index", id = UrlParameter.Optional)
    

    URL:http://localhost/Albums 在我的“相册”区域中触发我的“相册控制器”的“索引”操作。

    你的结构是什么样的?

    【讨论】:

    • 将 MapRoute 更改为没有任何效果。我添加了他的路由调试器 DLL,当我转到 /secure 时它说它匹配,即使没有调试器 DLL 我得到 404:匹配的路由:安全/{controller}/{action}/{id} True Secure/{controller} /{action}/{id} controller = Home, action = Index, id = System.Web.Mvc.UrlParameter (null) Namespaces = System.String[], area = Secure, UseNamespaceFallback = False
    • 命名空间可能是个问题吗?我的 SecureAreaRegistration 类位于命名空间 TothSolutions.Web.Areas.Secure 中,而我的 HomeController 类位于命名空间 TothSolutions.Web.Areas.Secure.Controllers 中。
    • 否 - 命名空间是正确的。我将用我的区域结构更新我的答案。
    • 我的看起来一样,除了安全而不是相册。 :( 最糟糕的是,当我浏览到 localhost/TothSolutions/secure 时,路由测试器对 Url“安全”的“匹配当前请求”说“真”,非常难过...
    • 另外我应该指出,我尝试使用 Visual Studio 服务器而不是 IIS - localhost:99/Secure - 并且给出相同的 404...
    【解决方案3】:

    我得到它的工作,答案是将区域注册类更改为:

    context.MapRoute(
                    "Secure_Default", // Route name
                    "Secure/{controller}/{action}/{id}", // URL with parameters
                    new { area="Secure", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                    new[] { typeof(Controllers.HomeController).Namespace }
                );
    

    我不知道为什么它适用于其他开发人员而不适用于我,而且我觉得当你创建一个新区域时它应该“开箱即用”,你不应该摆弄路线映射.

    无论如何,感谢大家的帮助,我正在为 RPM 的所有辛勤工作给出答案。

    【讨论】:

      【解决方案4】:

      您是否在 global.asax 中调用 AreaRegistration.RegisterAllAreas()?

      Sub Application_Start()
          AreaRegistration.RegisterAllAreas()
      
          RegisterRoutes(RouteTable.Routes)
      End Sub
      

      【讨论】:

      • 很确定这是在创建新的 MVC2 Web 应用程序时默认放置的。当然,除非他手动删除了它。
      【解决方案5】:

      您是否尝试过使用 http://localhost/myapp/Secure/Home/index ?我发现很多次,当我使用索引作为视图名称并且不在路径中指定它时,它永远不会起作用。它应该可以工作,但它对我不起作用。

      反正我不喜欢调用我的视图索引,所以对我来说没什么大不了的。

      【讨论】:

        猜你喜欢
        • 2011-01-01
        • 2011-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-16
        • 2010-12-12
        相关资源
        最近更新 更多