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

MVC 3 RoutingSystem

2013年03月04日 ⁄ 综合 ⁄ 共 2578字 ⁄ 字号 评论关闭

The routing system has two functions:

1. Examine an Incoming URL and figure out for which controller and action the request is intended.

2.Generate outgoing URLs.These are URLs that appear in the HTML rendered from our views so that a specific action will be invoked when the user clicks the link.

 

The Routing System Assembly:

Althoug the routing system is needed by the ASP.NET MVC Framework,it is intended to be used with other APS.NET technologies as well,including WEB FORMs.Because of this, the routing system classes are in the System.Web assembly and not in Syetem.Web.Mvc;

 

Creating the Routing Project

Routes are defined in Global.asax.The routing system doesn't have any special knowledge of controller and actions.It just extracts values for the segment variables and passes them along the request pipeline.It is later in the pipeline,when the request reaches the MVC Framewrok proper,that meaning is assigned to the controller and action variables.This is why routing system can be used with WebForms and how we are able to create our own variables.

 

Creating and Registering a Simple Route

There are two ways to define a Router:

public static void RegisterRoutes(RouteCollection routes) {
Route myRoute = new Route("{controller}/{action}", new MvcRouteHandler());
routes.Add("MyRoute", myRoute);
}

This is the first way ,Here is other way:

public static void RegisterRoutes(RouteCollection routes) {
routes.MapRoute("MyRoute", "{controller}/{action}");
}

Tip:Naming your routes is optional,and there is an argument that doing so sacrifices some of the clean separation of concerns that otherwise comes from routing.We are pretty relaxed about naming,but we explain why this can be a problem in the "Generating a URL from a Specific Route" section later in this chapter.

 

Definging Default Values

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

 

Using static URL Segments

Not all of the segments in a URL pattern need to be variables.You can also create patterns that have static segtments. Suppose we want to match a URL like this to support URLs that are prefixed with PUBLIC;

Http://mydomain.com/Public/Home/Index

We can do so by using a pattern like the one shown in Listing blow.

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

 

Defining Custom Segment Variables

Caution:Some names are reserved and not avaliable for custom segment variable naems.These are controller,action,and area.The meaning of the first two are obvious,and we will explain the role of areas in the "Working with Areas" section later in this chapter.

 

Using Custom Variables as Action Method Parameters

Using the RouteData.Values property is only one way to access custom route variables.The other way is much more elegant.

抱歉!评论已关闭.