c# - Asp.net MVC 2 领域。只有一个会起作用

标签 c# asp.net-mvc asp.net-mvc-2

这是我的目标:

我的 MVC 网络应用程序需要两个(或更多)“区域”。他们将像这样访问:

/* Home */
http://example.com/
http://example.com/about
http://example.com/faq
http://example.com/contact

/* Admin */
http://example.com/admin
http://example.com/admin/login
http://example.com/admin/account
http://example.com/admin/ect

喜欢按如下方式组织项目:

MyExampleMVC2AreasProject/
    Areas/
        Admin/
            Controllers/
            Models/
            Views/
                Home/
                Shared/
                    Site.Master
                Web.Config
            AdminAreaRegistration.cs
        Web/
            Controllers/
            Models/
            Views/
                Home/
                Shared/
                    Site.Master
                Web.Config
            WebAreaRegistration.cs
    Global.asax
    Web.Config

因此,在 Global.asax 中我有:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults

        );
    }
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }

这是WebAreaRegistration.cs

using System.Web.Mvc;

namespace MyExampleMVC2AreasProject.Areas.Web
{
    public class WebAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Web";
            }
        }

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

“AdminAreadRegistration.cs”设置相同,但 url 参数是 Admin/{action}/{id}

通过 Web 上面的设置,“区域”效果很好(example.com/about、example.com/contact 等)。

我需要做什么才能让 Admin“区域”按照我想要的方式连接到路线?我只是得到 404现在编辑。
我已经尝试了我能想到的所有路由组合、带 namespace 的路由、URL 参数、参数默认值等。 我觉得我缺少一些非常基本的东西。

最佳答案

我使用 this AreaRegistrationUtil class .它会自动在您指定的任何程序集中注册任何继承 AreaRegistration 的内容。作为一个额外的好处,它比 AreaRegistration.RegisterAllAreas 快得多,因为它只查看您指定的程序集。

关于c# - Asp.net MVC 2 领域。只有一个会起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4414120/

相关文章:

c# - Fire and Forget 没有得到安排

c# - 如何在silverlight中弹出居中的PopUp控件

c# - 显示换行符 asp.net mvc razor

visual-studio-2010 - "Run Custom Tool"用于 MVC2 项目中的 resx 文件(来自外部应用程序/脚本)

c# - ASP.NET MVC2-try/catch(throw?) block 的数量和位置

C# 将 XML 数据绑定(bind)到 ListView WPF

c# - MySQL C#错误; "connection must be valid and open"

javascript - Backbone js批处理保存

c# - 如何将文件发布到 asp.net mvc 应用程序?

jquery - 生成的 JsonResult 中的属性名称大小写不一致