ASP.NET MVC 2 区域,奇怪的路由行为

发布于 2024-08-30 06:50:02 字数 782 浏览 3 评论 0原文

我创建了一个名为“Admin”的区域。我还在这个区域创建了一个控制器(页面)和一个视图(列表)。

当我运行我的应用程序并输入 url“/Admin/Pages/List”时,我收到“找不到资源”错误。

当我进入 /Pages/List 时,Action 方法被命中,但找不到视图,因为应用程序正在错误的目录中搜索

~/Views/Pages/List.aspx 〜/视图/页面/List.ascx 〜/视图/共享/List.aspx ~/Views/Shared/List.ascx

视图位于 /Admin/Pages/List 中。

我的管理区域路由配置:

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller= "Pages",action = "Index", id = "" }
        );
    }
}

I've created an Area named "Admin". I've created also a controller(Pages) and a view(List) in this areas.

When I run my app and enter the url "/Admin/Pages/List" I'm getting an The resource cannot be found error.

When I enter /Pages/List, the Action method is hit but the view is not found,because the app is searching in wrong directories

~/Views/Pages/List.aspx
~/Views/Pages/List.ascx
~/Views/Shared/List.aspx
~/Views/Shared/List.ascx

the view is in /Admin/Pages/List.

My routing conf for Admin area:

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller= "Pages",action = "Index", id = "" }
        );
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2024-09-06 06:50:02

您是否已将 AreaRegistration.RegisterAllAreas(); 添加到 Global.asax.cs

这应该在现有的 routes.MapRoute 调用

编辑:

之前 运行查看我的管理区域,路由如下所示:

context.MapRoute(
                "Admin_Default",
                "Admin/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );

Have you added AreaRegistration.RegisterAllAreas(); to the Global.asax.cs

This should run before your existing routes.MapRoute calls

Edit:

Just looked at my Admin Area and the routing looks like this:

context.MapRoute(
                "Admin_Default",
                "Admin/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文