ASP.NET MVC Area操作
- 新建 Area:右键 -> Add –> Area
- 继承 AreaRegistration,重写AreaName属性与RegisterArea方法
public class BookStudyAreaRegistration : AreaRegistration { public override string AreaName { get { return "BookStudy"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "BookStudy_default",
"BookStudy/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } }
- (附:一般的路由映射)
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url:
"{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new string[] { "MyKB.Controllers" } ); } }
请注意与RegisterArea的url参数进行对比,只多了一个“BookStudy”,即AreaName。——这也是将Area称之为子站的原因。 - 在 Global 中注册此 Area
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); // here FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
- 以上所有步骤在VS2013或VS2012中均自动生成。