<Pro ASP.NET MVC 5> - Note01

50 篇文章 0 订阅
17 篇文章 0 订阅



CHAPTER 16 Advanced Routing Features


Working with Areas


to customize the routing system by replacing the standard MVC routing implementation classes and use the MVC Framework areas feature, which allows you to break a large and complex MVC application into manageable chucks.


Creating an Area

select Add->Area from the pop-up menu

Inside the Areas/Admin folder, you will see a mini-MVC project.

The Areas folder also contains a file called AdminAreaRegistration.cs,

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

this method registers a route with the URL pattern Admin/{controller}/{action}/{id}.

Visual Studio added a statement to the Global.asax file that takes care of setting up areas when it created the project,

protected void Application_Start() {
	AreaRegistration.RegisterAllAreas();
	RouteConfig.RegisterRoutes(RouteTable.Routes);
}

working inside an area is just the same as working in the main part of an MVC project.

If you navigate to the /Home/Index URL, you will see the error

public static void RegisterRoutes(RouteCollection routes) {
	routes.MapMvcAttributeRoutes();
	routes.MapRoute("MyRoute", "{controller}/{action}");
	routes.MapRoute("MyOtherRoute", "App/{action}", new {controller = "Home" });
}


because there are no namespace restrictions in place for this route and the MVC Framework can see two HomeController classes. To resolve this, I need to prioritize the main controller namespace in all of the routes which might lead to a conflict

routes.MapRoute("MyRoute", "{controller}/{action}", null, new[] {"UrlsAndRoutes.Controllers"});
routes.MapRoute("MyOtherRoute", "App/{action}", new {controller = "Home" }, new[] { "UrlsAndRoutes.Controllers" });

Creating Areas with Attributes

You can also create areas by applying the RouteArea attribute to controller classes


[RouteArea("Services")]
[RoutePrefix("Users")]
public class CustomerController : Controller {}

The RouteArea attribute moves all of the routes defined by the Route attribute into the specified area. The effect of this attribute combined with the RoutePrefix attribute means that to reach the Create action method, for example, I have to create a URL like this:

The effect of this attribute combined with the RoutePrefix attribute means that to reach the Create action method, for example, I have to create a URL like this: http://localhost:34855/Services/Users/Add/Adam/100

The RouteArea attribute doesn't affect routes that are defined by the Route attribute that start with ~/

The RouteArea has no effect on action methods to which the Route attribute has not been defined, which means that the routing for the List action method is determined by the RouteConfig.cs file and not attribute-based routing.


Generating Links to Actions in Areas

You do not need to take any special steps to create links that refer to actions in the same MVC area that the current request relates to. The MVC Framework detects that a request relates to a particular area and ensures that outbound URL generation will find a match only among routes defined for that area.


To create a link to an action in a different area, or no area at all, you must create a variable called area and use it to specify the name of the area you want, like this:

...
@Html.ActionLink("Click me to go to another area", "Index", new {area = "Support" })

If you want to link to an action on one of the top-level controllers (a controller in the /Controllers folder), then you should specify the area as an empty string, like this:


...
@Html.ActionLink("Click me to go to another area", "Index", new {area = "" })







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET MVC 5 框架揭秘》以一个模拟ASP.NET MVC内部运行机制的“迷你版MVC框架”作为开篇,其目的在于将ASP.NET MVC真实架构的“全景”勾勒出来。接下来本书以请求消息在ASP.NET MVC框架内部的流向为主线将相关的知识点串连起来,力求将”黑盒式”的消息处理管道清晰透明地展示在读者面前。相信精读本书的读者一定能够将ASP.NET MVC从接收请求到响应回复的整个流程了然于胸,对包括路由、Controller的激活、Model元数据的解析、Action方法的选择与执行、参数的绑定与验证、过滤器的执行以及View的呈现等相关的机制具有深刻的理解。 本书以实例演示的方式介绍了很多与ASP.NET MVC相关的很好实践,同时还提供了一系列实用性的扩展,相信它们一定能够解决你在真实开发过程中遇到的很多问题。本书末章提供的案例不仅仅用于演示实践中的ASP.NET MVC,很多的架构设计方面的东西也包含其中。除此之外,本书在很多章节还从设计的角度对ASP.NET MVC的架构进行了深入分析,所以从某种意义上讲本书可以当成一本架构设计的书来读。 ASP.NET MVC 5 框架揭秘 目录 第1章 ASP.NET + MVC 第2章 路由 第3章 Controller的激活 第4章 Model元数据的解析 第5章 3个描述对象 第6章 Model的绑定(上篇) 第7章 Model的绑定(下篇) 第8章 Model的验证(上篇) 第9章 Model的验证(下篇) 第10章 Action方法的执行 第11章 View的呈现 第12章 过滤器 第13章 特性路由 第14章 案例实践

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值