【ASP.NET MVC系列】浅谈ASP.NET MVC 路由

时间:2019-11-05 08:42来源:永利皇宫手机版网址
【02】  定义:性子路由就是将路由U翼虎L定义在调节器上或调节器内的主意上,而不像古板路由将路由URubiconL定义在RouteConfig.cs中。对举个例子下 可以总结为如下流程图:    第一个重

图片 1

【02】

 定义:性子路由就是将路由U翼虎L定义在调节器上或调节器内的主意上,而不像古板路由将路由URubiconL定义在RouteConfig.cs中。对举个例子下

可以总结为如下流程图:

 图片 2

图片 3

 第一个重载版本接纳当前的RequestContext,以致由客商钦赐的路由值(字典)。

自然, 你也得以友善下载RouteDebugger.dll, 援引到web项目中, 然后手动在web.config中投入

        ASP.NET 路由分歧于其余 U奥迪Q3L 重写方案。U奥迪Q7L 重写通过在将呼吁发送到网页以前实际改革 U安德拉L 来拍卖传入必要。举例,多少个使用 URL 重写的应用程序可能会将 URubiconL 从 /Products/Widgets/ 纠正为 /Products.aspx?id=4。此外,UCR-VL 重写平常未有对景挂画的 API 来创立基于格局的 U奥德赛L。在 UCR-VL 重写中,如若改换了 UCR-VL 形式,则必得手动更新包涵原始 U中华VL 的有所超链接。由于 ASP.NET 路由得以从 URAV4L 提取值,所以拍卖传入央浼时不改进U景逸SUVL。假使必需创立一个 U宝马7系L,则将参数值传递到为您生成 U君越L 的章程中。若要纠正 U冠道L 形式,请在某地点变动该情势,您在应用程序中成立的依靠该情势的保有链接将电动使用新形式。

译文:UXC60L格局恐怕带有文字值和变量占位符(称为U冠道L参数)。文字和占位符位于U福睿斯L的片段中,由斜杠(/)字符分隔。

If you want to add custom routes in an MVC application, you use the MapRoute(RouteCollection, String, String) method instead of the MapPageRoute(String, String, String) method.
译文:要是你想在MVC应用程序中增添自定义路由,您能够选用MapRoute(RouteCollection、String、String)方法,实际不是MapPageRoute(字符串、字符串、字符串)方法。

译文:上边的身先士卒彰显了咋样利用MapPageRoute方法成立一条渠道,该路由节制了地段和年参数中能够包罗的值。(在MVC应用程序中,您将选拔map路由艺术。)

图片 4

 Typical URL Patterns in MVC Applications

测量试验结果

图片 5

View Code

译文:您还是能够钦命路由不应该管理有个别ULX570L央求。通过定义一条路由,并点名StopRoutingHandler类应该用于拍卖该格局,进而防止了管理某个央浼的路由。当二个StopRoutingHandler对象管理央浼时,StopRoutingHandler对象会卡住需要作为路由的别样附加管理。相反,央求是作为ASP.NET 页面来拍卖的,网络页面,Web服务,或任何ASP.NET端点。您能够使用 RouteCollection.Ignore方法。

译文:带有形式能源的路由axd/pathInfo,被用于幸免Web财富文件的央求,比方WebResource,axd或ScriptResource传递到调控器。对于IIS 7.0,没有必要此外文件名称增添。对于IIS 6.0,您必得加多.mvc文件扩展名到U福特ExplorerL方式中,如上边包车型大巴事例:

图片 6图片 7

【13】

1 public static void RegisterRoutes(RouteCollection routes)
2 {
3   routes.Ignore("{resource}.axd/{*pathInfo}");
4 }

   (2)若是RouteTable中有一个路由成功相称,路由模块就能从相称成功的路由中得到IRouteHandler接口对象。

译文:上边包车型大巴亲自过问体现了来自.Globalasax文件的代码,该代码增多了多个路由对象,该对象定义了八个名称为action和项目称号的UENCOREL参数。具备钦赐形式的url被定向到名字为分类.aspx的大要页面。

图片 8

A Physical File is Found that Matches the USportageL Pattern(找到与URL形式匹配的情理文件)

/Controllers/NewFolder1/RouteDemo

Sometimes you have to handle URL requests that contain a variable number of URL segments. When you define a route, you can specify that if a URL has more segments than there are in the pattern, the extra segments are considered to be part of the last segment. To handle additional segments in this manner you mark the last parameter with an asterisk (*). This is referred to as a catch-all parameter. A route with a catch-all parameter will also match URLs that do not contain any values for the last parameter. The following example shows a route pattern that matches an unknown number of segments.

【ASP.NET MVC系列】浅谈ASP.NET MVC 路由。第三个重载选拔多个参数,当中第一个参数是路由名称。在路由会集西路由名称是唯黄金年代的,也正是说,未有三个不等的路由具备相近的名号。当钦命了路由名称时,路由集合就不须要循环遍历种种路由,相反,它能够马上找到钦点名称的路由,并移向上面包车型客车手续(2)。倘若找到的路由不能够相称钦命的参数,该方法就能回来空值,况且不再相称其余路由。

译文:您能够通过包罗常量来防止情势中的歧义,比方annual/{report}/{year} and monthly/{report}/{year}/{month}。

     (1)  域名便于记念和拼写;

译文:MVC运用程序中的杰出形式

 

抑或也得以动用NuGet很平价的装置RouteDebugger,在档案的次序方面右键-"Manage NuGet Packages"-"Online"输入"RouteDebugger"然后"Install".

 从内容上来看,个性路由的内容与古板路由大致,同样有路由U君越L、路由值、调整器路由、路由约束和路由默许值等。鉴于篇幅节制,在这里间就简单的概述一下,不作详细解说,前期有的时候间也许有心上人须求,再论述。

       注释:资源是贰个抽象概念,既可以够指叁个文本,也足以指方法调用的结果或服务器上的有的别样内容。

1.4 U智跑L应持有特色

 由于篇幅有限,路由章节就写到那,当然,还大概有众多内容没写,如路由铺排文件、路由平安设置、自定义路由约束等,留给读者对象们去商量吗。

id是bikes.

 3.1 U悍马H2L生成的高层系概述

 2.10 Scenarios When Routing Is Not Applied(不应用路由时的气象)

【ASP.NET MVC系列】浅谈ASP.NET MVC 路由。In addition to matching a URL request to a route definition by the number of parameters in the URL, you can specify that values in the parameters meet certain constraints. If a URL contains values that are outside the constraints for a route, that route is not used to handle the request. You add constraints to make sure that the URL parameters contain values that will work in your application.

译文:在MVC运用程序中,路由U昂科雷L方式平时包括调节器和动作占位符。

【08】

When a request is made, the URL is parsed into segments and placeholders, and the variable values are provided to the request handler. This process is similar to the way the data in query strings is parsed and passed to the request handler. In both cases variable information is included in the URL and passed to the handler in the form of key-value pairs. For query strings both the keys and the values are in the URL. For routes, the keys are the placeholder names defined in the URL pattern, and only the values are in the URL.

9   版权

【ASP.NET MVC系列】浅谈ASP.NET MVC 路由。 【01】

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using System.Web.Routing;
 7 
 8 namespace MVCDemo
 9 {
10     public class RouteConfig
11     {
12         public static void RegisterRoutes(RouteCollection routes)
13         {
14             routes.MapMvcAttributeRoutes();//特性路由
15 
16             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");//忽略资源文件
17 
18             routes.MapRoute(
19                 name: "Default",//路由名,唯一
20                 url: "{controller}/{action}/{id}",//路由URL
21                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },//路由URL默认值
22                 namespaces: new[] { "MVCDemo.Controllers" },//命名空间避免二义性
23                 constraints: new { id = @"^d*$" }//约束
24 
25                 );
26         }
27     }
28 }

Routing Is Explicitly Disabled for a U奥德赛L Pattern(为UEscortL方式显式禁止使用路由)

2.12 路由二义性

 

译文:在少数景况下,即便在Web站点启用时,ASP.NET 路由也不管理乞请。本有的叙述了一些场景,此中路由不管理央求。

 If you adopt the MVC convention of implementing controllers by creating classes that derive from the ControllerBase class and giving them names that end with "Controller", you do not need to manually add routes in an MVC application. The preconfigured routes will invoke the action methods that you implement in the controller classes.

2.4 URL Patterns(URL模式)

 6.2 Class Reference( 路由涉及到的重大类)

Index

4.2 路由数量

  • The route patterns that you have defined or the default route patterns, if any, that are included in your project type. 

  • 译文:您曾经定义的路由情势或暗许的路由情势,假如有的话,它们包蕴在你的种类项目中。

  • The order in which you added them to the Routes collection.
  •  译文:您将它们增进到路由集合的依次。

图片 9

   (5)在ASP.NET MVC中,IRouteHandler是MvcRouteHandler类的贰个实例,MvcRouteHandler转而回到一个兑现了IHttpHandler接口的MvcHandler对象。重临的MvcHandler对象主要用来实例化调整器,并调        用该实例化的调节器上的操作方法。

图片 10

【05】

图片 11

  • 感激您的翻阅,若有白璧微瑕,应接指教,协同学习、协作进步。
  • 博主网站:
  • 极少一些小说利用读书、参谋、援用、抄袭、复制和粘贴等二种方式组成而成的,大多数为原创。
  • 如您爱怜,麻烦推荐一下;如你有新主见,迎接建议,邮箱:2015177728@qq.com。
  • 能够转发该博客,但必需著名博客来源。

     (2)  简短;

【ASP.NET MVC系列】浅谈ASP.NET MVC 路由。1.5  小结

    如URubiconL:{Controller}/{action}/{id}。当恳求/albums/list/123传开时,该路由就能尝试相称传入的央求,如若匹配成功,它就成立一个字典,此中带有了从U途锐L中分析出的音信。确切地讲,路由还恐怕会向Values字典中为U奥迪Q3L中的每一个路由参数增添三个键;对于人生观路由{Controller}/{action}/{id},Values字典中应该最少含有八个键,分别是Controller,action和id,假如传入的UWranglerL是对/albums/list/123的乞求,路由就能够解析该诉求的U猎豹CS6L,并为字典的键提供值。本例中,字典中“Controller”键的值为albums,"action"键的值为“list”,"id"键的值是“123”;对于特性路由MVC使用DataTokens字典来储存更规范的音讯,并不是操作名称字符串。具体来说,它满含一个操作描述符列表,那些描述符直接指向路由相配时可能应用的操作方法。对于调控器等第的特点路由,列表旅长有持续四个操作。在一切MVC中都有接收的RequestContext的RouteData属性保存着外面路由值。

The following example shows code from a Global.asax file that adds a Route object that defines two URL parameters named action and categoryName. URLs that have the specified pattern are directed to the physical page named Categories.aspx.

译文:在Web Forms应用程序中,您能够选取路由接收类的MapPageRoute(字符串、字符串、字符串)方法创造路由。MapPageRoute方法成立三个路由对象并将其增加到RouteCollection对象。您能够在传递给MapPageRoute方法的参数中钦命路由对象的属性。

  3.2  U本田CR-VL生成详解

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using System.Web.Routing;
 7 
 8 namespace MVCDemo
 9 {
10     public class RouteConfig
11     {
12         public static void RegisterRoutes(RouteCollection routes)
13         {
14             routes.MapMvcAttributeRoutes();//
15 
16             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
17 
18             routes.MapRoute(
19                 name: "Default",
20                 url: "{controller}/{action}/{id}",
21                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
22             );
23         }
24     }
25 }

    调用GetRouteData方法会再次回到RouteData的三个实例。RouteData中蕴藏了关于相配伏乞的路由音讯。

2.5 Adding Routes to a Web Forms Application(增多路由到WebForm运用程序)

  路由的基本是二个极其简单的算法,该算法基于叁个由RouteCollection类和RouteBase类组成的简短抽象对象。能够动用两种措施来生成USportageL,但这一个措施都是调用RouteCollection.GetVirtualPath的多少个重载方法而截至。RouteCollection.GetVirtulPath方法公有七个重载版本,下边包车型地铁代码呈现它们的办法签字:

【10】

译文:暗中同意意况下,路由不管理映射到Web服务器上存活物理文件的呼吁。比方,倘诺存在 Products/Beverages/Coffee.aspx.物理文件,央浼 不被路由拍卖。尽管它与已定义的格局相相称,路由不管理央求,举个例子调节器/动作/id。
If you want routing to handle all requests, even requests that point to files, you can override the default behavior by setting the RouteExistingFiles property of the RouteCollection object to true. When you set this value to true, all requests that match a defined pattern are handled by routing.

For IIS 7.0, no file-name extension is needed. For IIS 6.0, you must add the .mvc file-name extension to the URL pattern, as in the following example:

 (3)一些路由或许带有未有对应路由参数的暗中认可值。如路由可能为category键提供默许值“pastries”,但是cateory不是路由UPAJEROL的二个参数,这种景色下,假使顾客传入的路由值字典为category提供了多少个值,那么该值必需相称category的私下认可值。

   (3)路由模块调用IRouteHandler接口的GetHandler方法,并赶回用来拍卖必要的IHttpHandler对象。

 URL patterns for routes in MVC applications typically include {controller} and {action} placeholders.

The route with the pattern {resource}.axd/{*pathInfo} is included to prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller.

行动是show.

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace MVCDemo.Controllers
 8 {
 9     public class RouteDemoController : Controller
10     {
11         //
12         // GET: /RouteDemo/
13         public ActionResult Index()
14         {
15             return View();
16         }
17     }
18 }

【09】

译文:假如您通过创造从调控器基类派生的类来完成调控器的MVC约定,并给它们以“调节器”结尾的称谓,那么你就不供给在MVC应用程序中手动加多路由了。预配置的路由将调用您在决定器类中贯彻的操作方法。

 图片 12

/Controllers/RouteDemo

图片 13

图片 14

译文:当路由拍卖UEscortL央求时,它尝试将呼吁的U哈弗L与路由特别。将ULX570L央浼般配到路由决计于以下条件:

   使用RouteDebugger调节和测验,启用RouteDebugger后,它会用四个DebuggerRouteHandler替换全体路由管理程序,DebugRouteHandler截获全体传入的呼吁,并查询路由表中的每四个路由,以便在页面尾巴部分展现路由的确诊数据和参数。为使用RouteDebugger,只需在VS的Package Manager Console窗口中应用NuGet安装就可以,命令Install-Package routedebugger.。RouteDebugger包在增加Route Debugger程序集的同一时间。也在web.config文件的appSettings节点中增加二个安装,用来展开或禁止使用路由调节和测验。

5 路由调节和测量检验

By default, routing does not handle requests that map to an existing physical file on the Web server. For example, a request for is not handled by routing if a physical file exists at Products/Beverages/Coffee.aspx. Routing does not handle the request even if it matches a defined pattern, such as {controller}/{action}/{id}.

     U传祺L (Uniform Resource Locator)代表联合营源定位符,互联英特网专门的学业财富之处;

  • 译文:您为有个别路由提供的别样暗许值。
  • Any constraints that you have provided for a route.

  • 译文:您为路线所提供的其余限制。

  • Whether you have defined routing to handle requests that match a physical file.

  • 译文:是或不是定义了路由来管理与物理文件相配的乞求。

设置教程如下:

 

        A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. A handler can also be a class that processes the request, such as a controller in an MVC application. To define a route, you create an instance of the Route class by specifying the URL pattern, the handler, and optionally a name for the route.

When a request is received, it is routed to the UrlRoutingModule object and then to the MvcHandler HTTP handler. The MvcHandler HTTP handler determines which controller to invoke by adding the suffix "Controller" to the controller value in the URL to determine the type name of the controller that will handle the request. The action value in the URL determines which action method to call.

3 路由生成U昂CoraL

      You add the route to the application by adding the Route object to the static Routes property of the RouteTable class. The Routes property is a RouteCollection object that stores all the routes for the application.You typically do not have to write code to add routes in an MVC application. Visual Studio project templates for MVC include preconfigured URL routes. These are defined in the MvcApplication class, which is defined in the Global.asax file.

 6.1  路由涉及的命名空间

When routing handles URL requests, it tries to match the URL of the request to a route. Matching a URL request to a route depends on all the following conditions:

     (5)  应该是“可破解的”,客户能够通过移除UCR-VL的末尾,进而到达越来越高档案的次序的消息种类布局;

 1.2 U奥德赛I、U奥德赛L和UENCOREN三者之间的涉及图

测量试验结果

     (6)  长久、不能够纠正

You can also specify that routing should not handle certain URL requests. You prevent routing from handling certain requests by defining a route and specifying that the StopRoutingHandler class should be used to handle that pattern. When a request is handled by a StopRoutingHandler object, the StopRoutingHandler object blocks any additional processing of the request as a route. Instead, the request is processed as an ASP.NET page, Web service, or other ASP.NET endpoint. You can use the RouteCollection.Ignore method  

路线1:报告/年/月,年和月暗中认可值。

  • Any default values that you have provided for a route.

(1)路由会集通过RouteBase.GetVirtual帕特h方法遍历每种路由并访谈:“能够转变给定参数的UCR-VL吗”,那几个进度看似于在路由与传播央求相称时所使用的非常逻辑。

译文:当你定义叁个路由时,您可感觉贰个参数内定三个私下认可值。假如该参数的值未有包蕴在ULANDL中,则应用暗中认可值。通过将dictionary对象分配给route类的暗中同意属性,可感到路由安装私下认可值。下边的例子展现了什么通过行使MapPageRoute(字符串、字符串、字符串、布尔值、RouteValueDictionary)方法增添具备暗中同意值的路由。

 1 public static void RegisterRoutes(RouteCollection routes)
 2 {
 3     routes.MapPageRoute("",
 4         "Category/{action}/{categoryName}",
 5         "~/categoriespage.aspx",
 6         true,
 7         new RouteValueDictionary 
 8             {{"categoryName", "food"}, {"action", "show"}},
 9         new RouteValueDictionary 
10             {{"locale", "[a-z]{2}-[a-z]{2}"},{"year", @"d{4}"}}
11        );
12 }

图片 15

When ASP.NET routing handles a URL request, the route definition shown in the example produces the results that are listed in the following table.

译文:约束是经过动用正则表明式或行使完毕IRouteConstraint接口的目的来定义的。当将路由定义增添到路由集适这时候,通过创设四个包蕴验证测验的RouteValueDictionary对象来丰硕节制。字典中的键标记限制应用到的参数。字典中的值能够是意味着正则表明式的字符串,也能够是完成IRouteConstraint接口的对象。
If you provide a string, routing treats the string as a regular expression and checks whether the parameter value is valid by calling the IsMatch method of the Regex class. The regular expression is always treated as case-insensitive. For more information, see .NET Framework Regular Expressions.

       常常状态下,UEnclaveI代表同意财富标志符(Uniform Resource Identifier)。U牧马人I是标志了一个财富的字符串。从能力角度看,全部U普拉多L都以URAV4I。W3C认为“UCRUISERL是三个业余的概念,但它这几个有效:ULX570L是U奥迪Q7I的生机勃勃种类型,它通过代表本人的重要走访机制来标记能源”,换句话说,USportageI是某种能源的标志符,而UKoleosL则为获得该财富提供了现实的音信。

protected void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("",
        "Category/{action}/{categoryName}",
        "~/categoriespage.aspx");
}

注:该例子未有加入区域

2.2  ASP.NET 路由与 URL 重写

8 参照他事他说加以考察文献

1 @inherits System.Web.Mvc.WebViewPage
2 
3 <h2>RouteDebugger调试</h2>

2.7 Setting Default Values for U帕杰罗L Parameters(为U逍客L参数设置暗许值)

For MVC applications, overloads of the RouteCollectionExtensions.MapRoute method, such as MapRoute(RouteCollection, String, String, Object, Object), enable you to specify defaults.

      (1)从命名角度,UEvoqueI标记能源且唯意气风发,U翼虎L标志财富地址 ,U中华VN标志财富名称;

 天性路由是在ASP.NET MVC 5中新扩展的,古板路由ASP.NET MVC 第11中学就有了。

2.1  WebForm URL与Route URL

When you define a route, you can assign a default value for a parameter. The default value is used if a value for that parameter is not included in the URL. You set default values for a route by assigning a dictionary object to the Defaults property of the Route class. The following example shows how to add a route that has default values, by using the MapPageRoute(String, String, String, Boolean, RouteValueDictionary) method.

   (1)UrlRoutingModule尝试使用在RouteTable中注册的路由相配当前倡议。

      (2)从数学关系:UQashqaiI=UTiguanL+URAV4N+UHighlanderL∩UXC60N;超级轻松见到,UHavalL一定是U福特ExplorerI,但ULacrosseI不肯定是U奥德赛L,同理,U悍马H2N一定是URI,但U奥迪Q5I不必然是UTucsonN;

  当ASP.NET 管理乞请时,路由管道首要由以下几步组成:

【11】

When ASP.NET routing handles a URL request, the route definition shown in the example (with default values of food for categoryName and showfor action) produces the results that are listed in the following table.

   (4)调用HTTP管理程序中的ProcessRequest方法,然后把要拍卖的呼吁传递给它

【03】

RouteConfig.cs

2.11 How UGL450Ls Are Matched to Routes(url怎么着与路由极度)

2.3  路由定义

【12】

      本解析基于1.2 U本田UR-VI、U哈弗L和U牧马人N三者之间的关联图。

【14】

译文:下表呈现了默许的UEnclaveL形式,它显得了由暗中同意路由拍卖的U奥迪Q7L需要的现身说法。

【15】

For example, a URL that includes the URL path /Products is mapped to a controller named ProductsController. The value in the action parameter is the name of the action method that is called. A URL that includes the URL path /Products/show would result in a call to the Showmethod of the ProductsController class.

For example, suppose that you add routes with the following patterns:

路由2永世不会管理必要,因为路由1第大器晚成被相称,它连接相配也许在2号路线上中国人民解放军海军事工业程高校业作的乞请。央浼:

若果恳求中缺乏几个参数,则选拔暗中认可值。由此,它们得以招致一条相称您未有预料到的伸手的路由。比方,假若您增添了以下情势:

译文:

View Code

译文:假若你想要路由拍卖全部哀告,以致指向文件的伏乞,您能够透过安装RouteCollection对象的特性RouteExistingFiles为true,  以此来覆盖私下认可值。当您将以此值设置为true时,匹配定义情势的有所伏乞都由路由拍卖。

  (1)开辟人士调用像Html.ActionLink或Url.Action之类的措施,这几个办法反过来再调用RouteCollection.GetVirtualPath方法,并向它传递三个RequestContext对象、多个包罗值的字典以至用于采纳生成UEnclaveL的路由名称(可选参数)。

图片 16

 

译文:比如,二个含有U揽胜L路线  /产品的U逍客L映射到叁个名字为ProductsController的调控器。action参数中的值是调用的操作方法的名目。四个包蕴U奥迪Q5L路径  /产物/show  的UEnclaveL会促成对ProductsController类的Showmethod的调用。

  译文:路线2设置为 products/show/{id}
Route 2 will never handle a request because Route 1 is evaluated first, and it will always match requests that could also work for Route 2. A request for seems to match Route 2 more closely, but it is handled by Route 1 with the following values:
controller is products.
action is show.
id is bikes.

Typically, you add routes in a method that is called from the handler for the Application_Start event in the Global.asax file. This approach makes sure that the routes are available when the application starts. It also enables you to call the method directly when you unit-test the application. If you want to call a method directly when you unit-test the application, the method that registers the routes must be static (Shared in Visual Basic) and must have a RouteCollection parameter.

     (2)ASP.NET MVC中,平常地,U瑞虎L映射并不是映射到具体磁盘上的大意文件,这是遵照ASP.NET路由天性,首要有三个进度,即URL映射和U奥迪Q3L生成(接下去讲授卡塔尔国。在ASP.NET路由中,您能够定义USportageL形式,该方式包涵在管理UQashqaiL央求时使用的值的占位符。在运转时,运用程序名称后边的U大切诺基L部分根据你所定义的UOdysseyL格局解析为离散值。举例,在伏乞   

 4.1 高档期的顺序央浼的路由管道

图片 17

The following example shows the code that creates default MVC routes in the Global.asax file, as defined in the Visual Studio project template for MVC applications.

In a URL pattern, you define placeholders by enclosing them in braces ( { and } ). You can define more than one placeholder in a segment, but they must be separated by a literal value. For example, {language}-{country}/{action} is a valid route pattern. However, {language}{country}/{action} is not a valid pattern, because there is no literal value or delimiter between the placeholders. Therefore, routing cannot determine where to separate the value for the language placeholder from the value for the country placeholder.

7 总结

下表展现了实惠的路由情势和与形式相称的U奇骏L央求示例。

     (3)  便于输入;

     (4)  可以反映出站点布局;

 

 先看看如下例子,你能完全驾驭啊?

1  public  VirtualPathData GetVirtualPath(RequestContext requestContex,RouteValueDictionary values)
2 
3  public  VirtualPathData GetVirtualPath(RequestContext requestContex,string name,RouteValueDictionary values)

   路由多个主要任务:相配传入的U冠道L央浼和布局与一定路由对应的U奥德赛L,总结之相称ULX570L和结构USportageL。在生成U福睿斯L时,生成UQX56L的情致应该率先与选取用来生成ULANDL的路由相相配,这样路由就能够在拍卖传入或传播的UF12berlinettaL时成为全体的双向系统。原则上,开辟人员应该提供生机勃勃组路由值,以便路由系统从当中选中第二个能够相称U大切诺基L的路由。

When routing handles a URL request, the route definition shown in the previous example produces the results that are listed in the following table.

/ query / { queryname } { * queryvalues }

路由2永世不会处理央求。第1条或然是指向每月报告的,而路由2恐怕是针对年度报告的。不过,路由第11中学的默许值意味着它将十三分任何或然用来路由2的乞求。

   译文: 路径1设置为{controller}/{action}/{id}
   Route 2 is set to products/show/{id}

【07】

二义性德姆o目录结构

译文:借使您提供三个IRouteConstraint对象,ASP.NET 路由通过调用IRouteConstraint对象的格外方法, 以此来检查参数值是还是不是可行。Match方法再次回到二个布尔值,该值表示参数值是还是不是行得通。
The following example shows how to use the MapPageRoute method to create a route that has constraints that limit what values can be included in the locale and year parameters. (In an MVC application, you would use the MapRoute method.)

大器晚成旦UTiguanL不包容在RouteTable集合中定义的其余路由对象,ASP.NET路由不管理央浼。相反,管理被传送给三个ASP.NET Page ,Web服务,或任何ASP.NET端点。

译文:譬喻,借使您加多了以下格局:
   Route 1 is set to {controller}/{action}/{id}

译文:除了通过UPRADOL中参数的数额来相配U智跑L乞请外,还足以钦定参数中的值满意一定的羁绊标准。倘使贰个ULX570L包罗的值大于了路由的牢笼,那么该路由就不会被用来拍卖央浼。您增加了大器晚成都部队分限制,以作保U福睿斯L参数包涵在您的应用程序中劳作的值。
Constraints are defined by using regular expressions or by using objects that implement the IRouteConstraint interface. When you add the route definition to the Routes collection, you add constraints by creating a RouteValueDictionary object that contains the verification test. The key in the dictionary identifies the parameter that the constraint applies to. The value in the dictionary can be either a string that represents a regular expression or an object that implements the IRouteConstraint interface.

View Code

 1.1 URI、URL和URN定义 

事例引入

译文:有的时候你必得管理包涵八个可变多少的U猎豹CS6L段的U揽胜极光L乞求,当中。当你定义叁个路由时,您能够钦定,倘诺一个U本田UR-VL的有个别比情势中有更加多的段,那么额外的段被认为是最终意气风发局部的意气风发局部。要以这种方式管理额外的段,您能够用星号(*)标志最终三个参数。那被称之为贰个含糊的参数。二个暗含全部参数的路由也将十分不带有最终三个参数的任何值的url。上边包车型客车自己要作为模范遵从规则展示了多个特未知数量的段的路由方式。

1 URI、URL与URN

译文:当发出供给时,U大切诺基L被深入剖判为一些和占位符,且变量值被提供给供给管理程序。那么些进度看似于查询字符串中的数据被拆解解析并传递给必要管理程序的法子。在此三种状态下,变量消息都包括在UENVISIONL中,并以键值对的款型传递给管理程序。对于查询字符串,键和值都在UEvoqueL中。对于路由,键是UEscortL方式中定义的占位符名称,在U传祺L中但是是值。

 (4)然后路由系统选取路由约束。

 只要启用RouteDebugger,它就显得从(在地点栏中)当前呼吁U哈弗L中领到的路由数据。如此,能够在地方栏中输入种种U奥迪Q7L,并查看输入的U昂科威L能与哪些路由极度,在页面底部,它还恐怕会来得一个分包应用程序定义的所用路由列表,这样能够查阅定义的哪位路由能够与近来UEnclaveL相相配。

图片 18图片 19

控制器products.

 (2)路由体制查看要求的路由参数(即未有提供路由参数的私下认可值),并保管提供的路由值字典为每一种渴求的参数提供贰个值,不然,URAV4L生成程序就能够即时停下,并回到空值。

 

译文:当接过到三个伸手时,它被路由到UrlRoutingModule对象,然后发送到MvcHandler HTTP管理程序。MvcHandler HTTP处理程序通过向U福特ExplorerL中的调节器值加多后缀“调控器”来规定要调用哪个调整器,以明确调控器的档期的顺序名称,该调整器将拍卖央求。U福睿斯L中的操作值决定调用哪个操作方法。

The following table shows valid route patterns and examples of URL requests that match the patterns.

2.8 Handling a Variable Number of Segments in a UOdysseyL 帕特tern(在UENCOREL形式中管理可变多少的段)

       译文:路由是意气风发种被映射到有个别管理程序的UKoleosL形式。管理程序也许是叁个物理文件,如在WebForms运用程序中的aspx文件。管理程序也也许是二个管理恳求的类,如MVC应用程序中的调控器。要定义路由,您可以因而点名U奥迪Q5L情势、处理程序和路线的称呼来创建路由类的实例。

A URL pattern can contain literal values and variable placeholders (referred to as URL parameters). The literals and placeholders are located in segments of the URL which are delimited by the slash (/) character.

3 路由概述(性情路由)

图片 20

译文:当ASP.NET路由拍卖几个UCRUISERL必要时,示例中所示的路由定义爆发了下表中列出的结果。

 在此意气风发章节,重要解析UCR-VL绑定到调节器操作的尾巴部分细节,深入分析底层原理,精通ASP.NET央求管道。注意,方今路由不仅只是ASP.NET MVC的贰个风味了,它早已超越这些界定,如ASP.NET Dynamic Data。路由既不包含MVC的在那之中级知识分子识,也不重视于MVC。

The following table shows the default URL patterns, and it shows examples of URL requests that are handled by the default routes.

2 路由概述(守旧路由)

【04】Professional Asp.net MVC 5

 如在VS二零一三选拔MVC模板创造项目时,自动生成类RouteConfig.

图片 21

You can avoid ambiguity in the patterns by including constants, such as annual/{report}/{year} and monthly/{report}/{year}/{month}.
If a URL does not match any Route object that is defined in the RouteTable collection, ASP.NET routing does not process the request. Instead, processing is passed to an ASP.NET page, Web service, or other ASP.NET endpoint.

视图=>别的窗口=>程序包管控台

     UEscortN(Uniform Resources Name)代表统一能源名称,互联英特网财富的称号;

 

6 与路由相关的关键命名空间和类

译文:假使您提供一个字符串,那么路由将字符串作为三个正则表明式来拍卖,并通过调用Regex类的IsMatch方法检查参数值是或不是有效。正则表明式总是被视为不区分轻重缓急写的。要打听更加多音信,请参阅。净框架正则表达式。
If you provide an IRouteConstraint object, ASP.NET routing checks whether the parameter value is valid by calling the Match method of the IRouteConstraint object. The Match method returns a Boolean value that indicates whether the parameter value is valid.

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("",
        "Category/{action}/{categoryName}",
        "~/categoriespage.aspx",
        true,
        new RouteValueDictionary 
            {{"categoryName", "food"}, {"action", "show"}});
}

译文:在ULacrosseL方式中,通过将它们封装在括号(以致)中来定义占位符。您能够在三个段中定义多少个占位符,可是必须用文字值分隔它们。例如,语言-国家/行动是意气风发种有效的不二秘籍方式。然则,语言国家/action不是贰个立见作用的格局,因为占位符之间从未文字值或分隔符。因而,路由无法调整将语言占位符的值与国家占位符的值分隔开分离。

图片 22图片 23

1.3 对U君越I、UEvoqueL和U帕杰罗N三者之间剖判

2.6 Adding Routes to an MVC Application

(5)路由特别成!以后得以因而查阅每叁个路由参数,并尝试利用字典中的对应值填充相应参数,从而生成U凯雷德L。

路子2:报告/年,年暗中同意值。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace MVCDemo.Controllers
 8 {
 9     public class RouteDemoController : Controller
10     {
11         //
12         // GET: /RouteDemo/
13         public ActionResult Index()
14         {
15             return View();
16         }
17     }
18 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using System.Web.Routing;
 7 
 8 namespace MVCDemo
 9 {
10     public class RouteConfig
11     {
12         public static void RegisterRoutes(RouteCollection routes)
13         {
14             routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
15 
16             routes.MapRoute(
17                 name: "Default", 
18                 url: "{controller}/{action}/{id}", 
19                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
20                 
21            );
22         }
23     }
24 }
25                 

只在同贰个减轻方案中,存在五个以至上述同等调控器下的相通action,当ULacrosseL要求时,会冒出二义性。

 译文:上边包车型地铁例证展示了在全局中开创私下认可MVC路由的代码。asax文件,正如在Visual Studio项目模板中定义的MVC应用程序。

     (1)WebForm中,对U翼虎L的传遍央求平时映射到磁盘上的情理文件,如.aspx文件。例如对

译文:

在窗口中输入:Install-Package routedebugger

(2)假诺叁个路由能够回答上边的难题(即相称),那么它就能够回来一个包蕴了USportageL的VirTualPathData实例以至其余相称的音信。不然,它就回去空值,路由体制移向列表中的下叁个路由。

Default values are used if a parameter is missing from the request. Therefore, they can cause a route to match a request that you did not expect. For example, suppose that you add routes with the following patterns:
Route 1: {report}/{year}/{month}, with default values for year and month.
Route 2: {report}/{year}, with a default value for year.
Route 2 will never handle a request. Route 1 might be intended for a monthly report, and Route 2 might be intended for an annual report. However, the default values in Route 1 mean that it will match any request that could also work for Route 2.

     UKoleosI(Uniform Resource Identifier)代表联合财富标记符,标记财富的字符串;

<add key="RouteDebugger:Enable" value="true'>

2.9  Adding Constraints to Routes(为路由增添封锁)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace MVCDemo.Controllers.NewFolder1
 8 {
 9     public class RouteDemoController : Controller
10     {
11         //
12         // GET: /RouteDemo/
13         public ActionResult Index()
14         {
15             return Content("路由二义性测试");
16         }
17     }
18 }

译文:平日地,在全局Global.asax文件中,您能够在叁个叫做Application_Start 方法里加多路由。该方法确认保证当应用程序运维时,路由是可以行使的。它还让你能够在对应用程序进行单元测量试验时间接调用该方式。假若您想在对应用程序进行单元测验时直接调用方法,那么注册路由的章程必得是静态的(在Visual Basic中是分享的),何况必须怀有三个路由参数。

【06】

In a Web Forms application, you create routes by using the MapPageRoute(String, String, String) method of the RouteCollection class. The MapPageRoute method creates a Route object and adds it to the RouteCollection object. You specify properties for the Route object in parameters that you pass to the MapPageRoute method.

      译文:你能够透过将路由对象增多到RouteTable类的静态路由属性中的方式将路由加多到应用程序中。路由属性是叁个为应用程序存款和储蓄全体路由的路由对象。在MVC应用程序中,您平常不须求编写制定代码来增多路由。VS项目模板为MVC包罗了事先陈设的URAV4L路由。那些都以在MvcApplication类中定义的,被定义在Global.asac 文件中。

     图片 24

RouteDemoController

<add key="RouteDebugger:Enabled" value="true" />
public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name 
            "{controller}/{action}/{id}",                           // URL with parameters 
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

Under some circumstances, ASP.NET routing does not handle a request even when is enabled for the Web site. This section describes some scenarios in which routing does not handle the request.

RouteConfig.cs

图片 25

4 路由绑定到操作

编辑:永利皇宫手机版网址 本文来源:【ASP.NET MVC系列】浅谈ASP.NET MVC 路由

关键词: