MVC 3 RoutingSystem

來源:互聯網
上載者:User

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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.