<Pro ASP.NET MVC 5> - Note01
来源:互联网 发布:怎么查看服务器端口 编辑:程序博客网 时间:2024/06/05 15:23
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 = "" })
- <Pro ASP.NET MVC 5> - Note01
- <Pro ASP.NET MVC 5> - Note02
- Pro ASP.NET Core MVC(二)【第一个MVC 应用程序】
- 《Pro ASP.NET MVC 3 Framework》译文目录
- Note on <Pro ASP.NET MVC 4> - 01
- Pro ASP.NET MVC –第五章 使用Razor
- Pro ASP.NET Core MVC(一)【前世今生】
- Pro ASP.NET Core MVC(四)【C#关键特征】
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- Note01
- hashtables之哈希桶法
- python常见HTTPError异常
- 记录mysql中的DATE_FORMAT()函数的使用
- 前端开发中的部分兼容性问题
- LeetCode第一题
- <Pro ASP.NET MVC 5> - Note01
- DVB-SI理解入门指南(一)(二)(三)
- 微信公众号开发---微信支付(JS API) JAVA
- JUnit4 执行测试时报initializationError错误
- spring视图国际化locale
- POJ 2566(尺取法+前缀和排序)
- 【Redis】事务和持久化
- viewport属性
- 如何在java中使用ConcurrentHashMap