MVC 3 RoutingSystem

本文深入探讨了ASP.NET MVC框架中的路由系统,包括其基本功能、组件、创建简单路由的方法、默认值定义、使用静态URL段、自定义变量设置以及如何通过路由生成特定链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

转载于:https://www.cnblogs.com/kfx2007/archive/2012/07/11/2586809.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值