这是我的目标:
我的 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/