<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 = "" })







0 0
原创粉丝点击