现在的位置: 首页 > 综合 > 正文

ASP.NET MVC Area操作

2012年09月20日 ⁄ 综合 ⁄ 共 1625字 ⁄ 字号 评论关闭

ASP.NET MVC Area操作

* 1、新建 Area:右键 -> Add -> Area...
* 2、继承 AreaRegistration,配置对应此 Area 的路由
* 3、在 Global 中通过 AreaRegistration.RegisterAllAreas(); 注册此 Area
* 4、有了 Area,就一定要配置路由的命名空间

using System.Web.Mvc;

namespace MVC20.Areas.AsynchronousController

{

// 新建一个 Area 会自动生成这个继承自 AreaRegistration 的类

// 如果需要使用此 Area 下的 MVC, 需要在 Global 中 AreaRegistration.RegisterAllAreas();

public class AsynchronousControllerAreaRegistration : AreaRegistration

{

public override string AreaName

{

get

{

return "AsynchronousController";

}

}

public override void RegisterArea(AreaRegistrationContext context)

{

// 在 Area 中配置路由的时候,要设置命名空间(即本例中的第 4 个参数)

context.MapRoute(

"AsynchronousController_default",

"AsynchronousController/{controller}/{action}/{id}",

new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults

new string[] { "MVC20.Areas.AsynchronousController.Controllers" }

);

}

}

}

Global.asax

代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing;

namespace MVC20

{

public class MvcApplication : System.Web.HttpApplication

{

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// 用于本项目中使用了 Area,所以在配置路由的时候,要设置命名空间(即本例中的第 4 个参数)

routes.MapRoute(

"Default", // Route name

"{controller}/{action}/{id}", // URL with parameters

new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // UrlParameter.Optional - 如果从url路由中无法获取某个参数的值,则从url参数中获取该参数的值

new string[] {"MVC20.Controllers"}

);

}

protected void Application_Start()

{

// 注册本应用程序中的所有 Area

AreaRegistration.RegisterAllAreas();

RegisterRoutes(RouteTable.Routes);

}

}

}

'mso-�-o83���0.5pt; font-family:"Segoe UI","sans-serif";color:black;background:#DDEDFB'> false;

}

return true;

}

}

抱歉!评论已关闭.