ASP.NET MVC 2领域404

时间:2010-06-10 04:11:26

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

有没有人能够让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 }
            );
        }

谢谢, 贾斯汀

5 个答案:

答案 0 :(得分:4)

我很确定这是因为你的区域注册类与托管它的项目位于不同的名称空间。它可以解释为什么你的解决方案有效 - 你用命令空间中的重载来注册它。我遇到了类似的问题,并通过更正命名空间来修复。

答案 1 :(得分:2)

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

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

然而生病了,尝试改变你的路线:

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

网址(<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)

网址:http://localhost/Albums触发我的“相册”区域中“AlbumsController”的“索引”操作。

你的结构是什么样的?

答案 2 :(得分:2)

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

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

我不知道为什么它适用于其他开发人员而不适合我,我觉得在创建新区域时它应该“开箱即用”,您不应该使用路径映射

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

答案 3 :(得分:0)

你在global.asax中调用AreaRegistration.RegisterAllAreas()吗?

Sub Application_Start()
    AreaRegistration.RegisterAllAreas()

    RegisterRoutes(RouteTable.Routes)
End Sub

答案 4 :(得分:0)

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

我不喜欢调用我的观点索引,所以对我来说没什么大不了的。

相关问题