MVC Area Usage

ASP.NET MVC Area操作

  1. 新建 Area:右键 -> Add –> Area
  2. 继承 AreaRegistration,重写AreaName属性与RegisterArea方法
    MVC Area Usage
    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 }
            );
        }
    }
    MVC Area Usage
  3. (附:一般的路由映射)
    MVC Area Usage
    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" }
            );
        }
    }
    MVC Area Usage
    请注意与RegisterArea的url参数进行对比,只多了一个“BookStudy”,即AreaName。——这也是将Area称之为子站的原因。
  4. 在 Global 中注册此 Area
    MVC Area Usage
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
    
        WebApiConfig.Register(GlobalConfiguration.Configuration); // here
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
    MVC Area Usage
  5. 以上所有步骤在VS2013或VS2012中均自动生成。

MVC Area Usage,布布扣,bubuko.com

MVC Area Usage

上一篇:Linux netstat命令详解


下一篇:用jquery实现html5的placeholder功能