事例引进,有的人不想令人家知道自身文件的门路公海赌船网址

2.3  路由定义

§8 URLs and Routing

Before ASP.NET MVC, the core assumption of routing in ASP.NET (just like
in many other web application platforms) was that URLs correspond
directly to files on the server’s hard disk. The server executes and
serves the page or file corresponding to the incoming URL. Table 8–1
gives an example

公海赌船网址 1

如此那般, 限制会相当多, 有的人不想让旁人通晓自个儿文件的不二等秘书诀,
或然是那几个代表方法太雅观了等等.

 

【03】https://msdn.microsoft.com/zh-cn/library/cc668201(v=vs.100).aspx

§8.1 Putting the Programmer Back in Control

ASP.NET MVC打破了这种规模, since ASP.NET MVC’s requests are handled by
controller classes (compiled into a .NET assembly), there are no
particular files corresponding to incoming
UEvoqueLs.所以那也就平昔不门路对应的特定文件了.

You are given complete control of your UPAJEROL schema—that is, the set of
U安德拉Ls that are accepted and their mappings to controllers and actions.
上边大家来探视mvc中是怎么定义路径的.

公海赌船网址 2

This is all managed by the framework’s routing
system.那全然是又框架的门路系统管理的.

 

§8.1.1 About Routing and Its .NET Assemblies

The routing system was originally designed for ASP.NET MVC, but it was
always intended to be shared with other ASP.NET technologies,
including Web Forms.路线系统本来是给mvc自个儿用的,
可是也会被其余asp.net技巧使用.
所以路线代码是位于贰个独自的顺序集里(System.Web.Routing.dll in .NET
3.5, and simply System.Web.dll in .NET 4),并不是在System.Web.Mvc.dll
中.

 

译文:假诺您提供贰个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.)

§8.2 Setting Up Routes

我们来探视路径的配备, 在global.asax.cs文件里

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );
        }

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

When the application first starts up (i.e., when Application_Start()
runs), the RegisterRoutes() method populates a global static
RouteCollection object called RouteTable.Routes. That’s where the
application’s routing configuration lives. The most important code is
that shown in bold: MapRoute() adds an entry to the routing
configuration. To understand what it does a little more clearly, you
should know that this call to MapRoute() is just a concise alternative
to writing the following:当应用程序运行的时候,也便是Application_Start()
鱼腥的时候, RegisterRoutes()
静态方法会装入一个名为RouteTable.Routes的大局静态RouteCollection对象.
也是放置路线配置的地点.MapRoute是路径配置的入口, 为了轻松的认证,
大家来举下边包车型客车事例

            Route myRoute = new Route("{controller}/{action}/{id}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new
                {
                    controller = "Home",
                    action = "Index",
                    id = UrlParameter.Optional
                })
            };
            routes.Add("Default", myRoute);

我们地方的看到的这段代码和体系自动生成的这段代码是一律的.

公海赌船网址 3

 

§8.2.1  Understanding the Routing Mechanism

The routing mechanism runs early in the framework’s request processing
pipeline. Its job is to take an incoming UENVISIONL and use it to obtain an
IHttpHandler object that will handle the request.
早起的路由机制运维在框架的央求处理管道. 它的办事正是运用进来的UEscortL,
并用它来博取三个力所能致处理诉求的的IHttpHandler 对象

Many newcomers to the MVC Framework struggle with routing. It isn’t
comparable to anything in earlier ASP.NET technologies, and it’s easy
to configure wrong. By understanding its inner workings, you’ll avoid
these difficulties, and you’ll also be able to extend the mechanism
powerfully to add extra behaviors across your whole
application.多数mvc框架的菜鸟对路由的概念改到很思疑.
因为它和今后任何的asp.net才具分裂,况兼很轻巧配置错误.
通过询问它的中间运行,大家就足以幸免那几个题目,你也得以透过扩张额外的一坐一起使全部应用程序扩充的更加强大.

The Main Characters: RouteBase, Route, and RouteCollection

路由配置重要有3个部分:

  • RouteBase is the abstract base class for a routing entry.
    You can implement unusual routing behaviors by deriving a custom
    type from it (I’ve included an example near the end of this
    chapter), but for now you can forget about it.
  • Route is the standard, commonly used subclass of RouteBase
    that brings in the notions of URL templating, defaults, and
    constraints. This is what you’ll see in most examples.
  • A RouteCollection is a complete routing configuration. It’s
    an ordered list of RouteBase-derived objects (e.g., Route
    objects).

How Routing Fits into the Request Processing Pipeline

When a U奥迪Q3L is requested, the system invokes each of the IHttpModules
registered for the application. 当一个U奥迪Q5L被呼吁,
系统调用每一个在应用程序中一度登记的IHttpModules
.个中二个就是UrlRoutingModule

** 

The Order of Your Route Entries Is Important

If there’s one golden rule of routing, this is it: put
more-specific route entries before less-specific
ones.
假如有个路由计划的纯金准绳: 那么正是将别树一帜路线放在一般路线的前边.
因为系统的恰当由的算法是从最上部初始, 实际不是找最契合的.

 

§8.2.2  Adding a Route Entry

默许的路由是很常见的, 假若你想要管理任何体系的ULANDL, 你还亟需做一些手脚.
小编来举个轻松的事例, 譬如大家想要用U福睿斯L /Catalog来查看那个体系的保有产品

            routes.Add(new Route("Catalog", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(
                new { controller = "Products", action = "List" }
                )
            });

大家能够用地方的这段代码来兑现我们的目的. 它能够支持我们落到实处/Catalog
或然是 /Catalog?some=querystring, 不过 /Catalog/Anythingelse
那样的url是可怜的.

 

URL Patterns Match the Path Portion of a URL

公海赌船网址 4

 

Meet RouteValueDictionary

A different technique to populate a RouteValueDictionary is to
supply an IDictionary<string, object> as a constructor
parameter, or alternatively to use a collection initializer, as in the
following example:

            routes.Add(new Route("Catalog", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary
                {
                    { "controller", "Products" },
                    { "action", "List" }
                }
            });

 

Take a Shortcut with MapRoute()

ASP.NET MVC adds an extension method to RouteCollection, called
MapRoute(). 你会发觉那比使用routes.Add(new Route(…)) 方便相当多.

            routes.MapRoute("PublicProductsList", "Catalog",
                new { controller = "Products", action = "List" });

In this case, PublicProductsList is the name of the route entry.
It’s just an arbitrary unique string. That’s optional.

 

§8.2.3  Using Parameters

As you’ve seen several times already, parameters can be accepted via a
curly brace syntax. 正像你眼下看到的, 参数能够放在{}里,
这里我们加三个color参数到路由中:

            routes.MapRoute(null, "category/{color}",
               new { controller = "Products", action = "List" });

This route will now match URLs such as /Catalog/yellow or
/Catalog/1234, and the routing system will add a corresponding
name/value pair to the request’s RouteData object. On a request
to /Catalog/yellow, for example, RouteData.Values[“color”] would be
given the value yellow
.

 

Receiving Parameter Values in Action Methods

You know that action methods can take parameters. When ASP.NET MVC
wants to call one of your action methods, it needs to supply a value
for each method parameter. One of the places where it can get values
is the RouteData collection. It will look in RouteData’s Values
dictionary, aiming to find a key/value pair whose name matches the
parameter name.

小编们精晓action方法能够带参数. 当mvc想要调用八个action方法,
它需求提供一个value给艺术的参数. 它获得value的多少个地点便是RouteData
collection.
它会在RouteData’s
的键值对中找找多个和参数名对应的value.
So, if you have an action method like the following, its color
parameter would be populated according to the {color} segment parsed
from the incoming URL:

为此, 倘使你有个action方法像下边那样的,
那么它的color参数就在传出的url中的{color}中

        public ActionResult List(string color)
        {
            // Do something
        }

To be more precise, action method parameters aren’t simply taken
directly from RouteData.Values, but instead are fetched via the
model binding system, which is capable of instantiating and
supplying objects of any .NET type, including arrays and collections.
You’ll learn more about this mechanism in Chapters 9 and 12.
更规范的说,
action方法的参数不仅只是简短的直白从RouteData.Values获取.
而是从模型绑定系统中拿走,各类.net类型. 你会在第9章和12章中打探越来越多.

 

§8.2.4  Using Defaults

You didn’t give a default value for {color}, so it became a mandatory
parameter. The Route entry no longer matches a request for /Catalog.
You can make the parameter optional by adding to your Defaults object:
在地点的例证中, 我们从没给{color}一个暗中同意值, 它成为了四个威吓的参数.
路由入口不再相称/Catalog 须要. 你可以

            routes.MapRoute(null, "Catalog/{color}",
                new { controller = "Products", action = "List", color = (string)null });

与上述同类, 路由就会相称/Category和/Category/orange了.

一经您想要贰个非null 的暗许值, 举例未有null的 int, 你能够显式的钦命值

            routes.Add(new Route("Catalog/{color}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(
                new { controller = "Products", action = "List", color = "Beige", page = 1 }
                )
            });

That’s a perfectly fine thing to do; it’s the correct way to set up
RouteData values that are actually fixed for a given Route
entry. For example, for this Route object,
RouteData[“controller”] will always equal “Products”, regardless
of the incoming URL, so matching requests will always be handled by
ProductsController.

那般,不管道输送入的url是怎样, 相配必要总会被ProductsController管理.

Remember that when you use MvcRouteHandler (as you do by default
in ASP.NET MVC), you must have a value called controller; otherwise,
the framework won’t know what to do with the incoming request and will
throw an error. The controller value can come from a curly brace
parameter in the URL, or can just be specified in the Defaults object,
but it cannot be omitted.

 

Creating Optional Parameters with No Default Value

就疑似私下认可的路由配置, 大家得以钦定暗许值UrlParameter.Optional.

            routes.MapRoute(null, "Catalog/{page}",
                new { controller = "Products", action = "List", page = UrlParameter.Optional });

这么, 当访谈的U奥迪Q5L有page值的时候, 我们就利用传入的vallue, 若无,
那么我们就不想action方法中传任何参数.你恐怕会狐疑,
为啥不用0只怕是null 作为暗许参数, 下边是它的八个原因:

  • If your action method takes a page parameter of type int, then
    because that type can’t hold null, you would have to supply the
    default value of 0 or some other int value. This means the action
    method would now always receive a legal value for page, so you
    wouldn’t be able to control the default value using the MVC
    Framework’s [DefaultValue] attribute or C# 4’s optional
    parameter syntax on the action method itself (you’ll learn more
    about these in the next chapter).
  • 要是您的action方法有个int类型的page参数,不过它是值类型,
    无法是null. 所以你必要提供三个暗许值(0大概是任何的值).
    那也认为着action方法总是要求三个官方的值, 所以,
    假诺action方法本身行使mvc框架的[defaultvalue]特征只怕是C#4的可选参数语法,
    你将不可能调整它的类型.(你会在接下去的一章中打探更加多)
  • Even if your action’s page parameter was nullable, there’s a
    further limitation. When binding incoming data to action method
    parameters, the MVC Framework prioritizes routing parameter values
    above query string values (you’ll learn more about value providers
    and model binding in Chapter 12). So, any routing value for
    page—even if it’s null—would take priority and hide any query
    string value called page.
  • 即时你的action的page参数能够是null类型. 这里还大概有个限制.
    当action方法的参数是binding类型的时候, mvc
    框架会将路由参数优先于查询字符串值.(你会在12章中学到值提供者和模型绑定).
    所以,
    任何为page设置的路由值–纵然是null–也会优先于访谈page的查询字符串

UrlParameter.Optional缓慢解决了这两个难点

 

§8.2.5  Using Constraints

不经常候, 你会想要增加额外的口径, 以十一分特定的route. 举例:

  • 您想相配get央求, 实际不是post央求
  • 有个别参数要协作特定的参数(e.g. ID参数必需合作数字类型)
  • 一对route用来相配常规的web浏览器发来的乞求,
    有的相配iphone发来的一模一样U传祺L

In these cases, you’ll use the Route’s Constraints property

 

Matching Against Regular Expressions

为了保证参数是数字类型的, 大家应用这样的条条框框:

            routes.MapRoute(null, "Articles/{id}",
                new { controller = "Articles", action = "Show" },
                new { id = @"\d{1,6}" });

那般, route就能相称 /Articles/1 和 /Articles/123456 这两体系型,
并非别的的,(这里的正则表明式表示的是: 数字类型,1~6个)

 

Matching HTTP Methods

If you want your Route to match only GET requests (not POST
requests), you can use the built-in HttpMethodConstraint class (it
implements IRouteConstraint)—for example:

            routes.MapRoute(null, "Articles/{id}", 
                new { controller = "Articles", action = "Show" },
                new { httpMethod = new HttpMethodConstraint("GET") });

您想相称什么样的HTTP方法, 就把它放到HttpMethodConstraint构造器中,
比如,new HttpMethodConstraint(“GET”, “DELETE”).

您要注意的是 HttpMethodConstraint[HttpGet] and
[HttpPost]
无关

 

Matching Custom Constraints

一旦前面的两种都无法满意你, 那么你还是能兑现它的.
举个例证,假诺您想创设三个只允许web浏览器步向的路由入口,
你能够创造如下的自律:

        public class UserAgentConstraint : IRouteConstraint
        {
            private string _requiredSubstring;
            public UserAgentConstraint(string requiredSubstring)
            {
                this._requiredSubstring = requiredSubstring;
            }
            public bool Match(HttpContextBase httpContext, Route route, string paramName,
            RouteValueDictionary values, RouteDirection routeDirection)
            {
                if (httpContext.Request.UserAgent == null)
                    return false;
                return httpContext.Request.UserAgent.Contains(_requiredSubstring);
            }
        }

上面包车型大巴路由只好合营由iphone发起的乞求:

            routes.Add(new Route("Articles/{id}", new MvcRouteHandler()){
                Defaults = new RouteValueDictionary(new { controller = "Articles", action = "Show" }),
                Constraints = new RouteValueDictionary(
                    new { id = @"\d{1,6}", userAgent = new UserAgentConstraint("iPhone") }});

 

§8.2.6  Prioritizing Controllers by
Namespace

§8.2.7  Accepting a Variable-Length
List of Parameters

§8.2.8  Matching Files on the Server’s
Hard Disk

§8.2.9  Using IgnoreRoute to Bypass the Routing System

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{filename}.xyz");
    // Rest of routing config goes here
}

Here, {filename}.xyz is treated as a URL pattern just like in a normal
route entry, so in this example,
the routing system will now ignore any requests for /blah.xyz or
/foo.xyz?some=querystring. (Of course,
you must place this entry higher in the route table than any other
entry that would match and handle
those URLs.) You can also pass a constraints parameter if you want
tighter control over exactly which
URLs are ignored by routing.

此处{filename}.xyz 被用作三个UCR-VL模型, 就如三个家常的路由入口. 

译文:

9   版权

【06】https://msdn.microsoft.com/en-us/library/cc668177.aspx

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:

     U奥迪Q5N(Uniform Resources
Name)代表统一财富名称,互联英特网财富的称呼;

<add key="RouteDebugger:Enabled" value="true" />

【12】https://msdn.microsoft.com/en-us/library/system.web.mvc.mvcroutehandler.aspx

 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.

 

1.5  小结

 只要启用RouteDebugger,它就显得从(在地方栏中)当前呼吁U奥迪Q3L中领到的路由数据。如此,能够在地方栏中输入各样UTiggoL,并查阅输入的U昂CoraL能与哪些路由极度,在页面尾巴部分,它还大概会显示叁个包蕴应用程序定义的所用路由列表,那样能够查阅定义的哪位路由能够与当前ULANDL相相称。

【15】https://msdn.microsoft.com/en-us/library/ee941656.aspx

公海赌船网址 5

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(字符串、字符串、字符串)方法。

1 @inherits System.Web.Mvc.WebViewPage
2 
3 <h2>RouteDebugger调试</h2>
1  public  VirtualPathData GetVirtualPath(RequestContext requestContex,RouteValueDictionary values)
2 
3  public  VirtualPathData GetVirtualPath(RequestContext requestContex,string name,RouteValueDictionary values)

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

控制器products.

(5)路由特别成!以往能够由此查阅每一个路由参数,并尝试采用字典中的对应值填充相应参数,从而生成USportageL。

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.

公海赌船网址 6

视图=>其余窗口=>程序包管控台

【05】http://www.cnblogs.com/liangxiaofeng/p/5620033.html

     
(2)从数学关系:UPRADOI=U凯雷德L+UKoleosN+UPRADOL∩UKoleosN;很轻便看到,URAV4L一定是UPRADOI,但U君越I不自然是U奥迪Q3L,同理,UEscortN一定是UWranglerI,但U哈弗I不肯定是UCR-VN;

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.

RouteDemoController

译文:在Web
Forms应用程序中,您可以使用路由采用类的MapPageRoute(字符串、字符串、字符串)方法创制路由。MapPageRoute方法创立一个路由对象并将其增添到RouteCollection对象。您能够在传递给MapPageRoute方法的参数中钦点路由对象的性质。

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

译文:当发出乞请时,UEvoqueL被解析为一些和占位符,且变量值被提要求央浼管理程序。这一个进度看似于查询字符串中的数据被深入分析并传递给乞请管理程序的不二诀窍。在那三种状态下,变量消息都蕴含在URubiconL中,并以键值对的样式传递给管理程序。对于查询字符串,键和值都在U奥迪Q7L中。对于路由,键是UENCOREL格局中定义的占位符名称,在U汉兰达L中只是是值。

测量检验结果

公海赌船网址 7

 

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

3 路由概述(特性路由)

  3.2  U迈凯伦570L生成详解

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.

公海赌船网址 8

 定义:个性路由就是将路由ULANDL定义在调整器上或调节器内的章程上,而不像守旧路由将路由USportageL定义在RouteConfig.cs中。对譬如下

公海赌船网址 9

译文:当你定义一个路由时,您可感觉一个参数钦定三个私下认可值。假使该参数的值没有包括在U大切诺基L中,则利用暗中认可值。通过将dictionary对象分配给route类的默许属性,可感到路由安装暗中认可值。上边包车型大巴例证浮现了如何通过应用MapPageRoute(字符串、字符串、字符串、布尔值、RouteValueDictionary)方法增添具有默许值的路由。

下表呈现了卓有作用的路由情势和与形式相配的U奥迪Q7L要求示例。

2.11 How U君越Ls Are Matched to
Routes(url如何与路由特别)

在窗口中输入:Install-Package
routedebugger

译文:

路由2长久不会管理诉求,因为路由1率先被相配,它连接相称可能在2号路线上干活的央浼。诉求http://server/application/products/show/bikes似乎比赛路线2更紧密,但它是由路线1以下值:

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

注:该例子没有参与区域

译文:当接受到一个呼吁时,它被路由到UrlRoutingModule对象,然后发送到MvcHandler
HTTP管理程序。MvcHandler
HTTP处理程序通过向UKugaL中的调节器值增加后缀“调控器”来鲜明要调用哪个调控器,以分明调整器的花色名称,该调控器将管理央浼。USportageL中的操作值决定调用哪个操作方法。

2.8 Handling a Variable Number of Segments
in a U奥迪Q3L Pattern(在USportageL格局中拍卖可变多少的段)

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

     U传祺L (Uniform Resource
Locator)代表统一财富定位符,互联英特网标准能源的地址;

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

公海赌船网址 10公海赌船网址 11

译文:USportageL方式或然带有文字值和变量占位符(称为U凯雷德L参数)。文字和占位符位于UCRUISERL的片段中,由斜杠(/)字符分隔。

5 路由调节和测量检验

     U奥迪Q7I(Uniform Resource
Identifier)代表统一能源标志符,标识财富的字符串;

译文:平时地,在大局Global.asax文件中,您能够在二个叫做Application_Start
方法里加多路由。该方法确认保障当应用程序运转时,路由是能够运用的。它还让你能够在对应用程序进行单元测验时一直调用该办法。假设你想在对应用程序进行单元测量试验时直接调用方法,那么注册路由的艺术必须是静态的(在Visual
Basic中是分享的),並且必得怀有七个路由参数。

 先看看如下例子,你能一心明了啊?

 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                 

View
Code

译文:在MVC运用程序中,路由UEvoqueL形式日常富含调节器和动作占位符。

译文:在U纳瓦拉L形式中,通过将它们封装在括号(以及)中来定义占位符。您能够在二个段中定义七个占位符,然而必得用文字值分隔它们。举例,语言-国家/行动是一种有效的门路情势。然则,语言国家/action不是贰个一蹴而就的方式,因为占位符之间从未文字值或分隔符。因而,路由不能垄断将语言占位符的值与国家占位符的值分隔开分离。

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

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

行动是show.

RouteConfig.cs

 (3)一些路由恐怕带有未有对应路由参数的默许值。如路由或然为category键提供暗许值“pastries”,可是cateory不是路由UWranglerL的一个参数,这种情状下,如若客户传入的路由值字典为category提供了叁个值,那么该值必得相配category的私下认可值。

【14】https://msdn.microsoft.com/en-us/library/bb398900.aspx

     
译文:你能够通过将路由对象添加到RouteTable类的静态路由属性中的方式将路由增添到应用程序中。路由属性是贰个为应用程序存款和储蓄全数路由的路由对象。在MVC应用程序中,您经常无需编写制定代码来加多路由。VS项目模板为MVC富含了先行计划的URAV4L路由。这个都以在MvcApplication类中定义的,被定义在Global.asac
文件中。

     (2)ASP.NET
MVC中,平时地,UEnclaveL映射而不是映射到具体磁盘上的情理文件,那是根据ASP.NET路由个性,重要有五个经过,即U奥迪Q3L映射和UWranglerL生成(接下去解说)。在ASP.NET路由中,您能够定义UWranglerL方式,该情势饱含在管理U揽胜极光L央浼时行使的值的占位符。在运维时,运用程序名称前边的ULacrosseL部分依照你所定义的UCRUISERL格局深入分析为离散值。举个例子,在伏乞http://server/application/Products/show/beverages时,路由分析器可以将值Products、show和beverages传递给请求的处理程序。相反,在一个不由URL路由管理的请求中,/Products/show/beverages片段将被解释为运用程序中的一个文件的路径。
  

2.1  WebForm URL与Route URL

二义性德姆o目录结构

 

View
Code

   
 (1)WebForm中,对U途达L的传播乞求平常映射到磁盘上的概略文件,如.aspx文件。比如对http://server/application/Product.aspx?id=4的请求映射到名为Products.aspx文件,该文件包含代码和标记用于呈现对浏览器的响应,一般请求示过程抽象如下:

译文:当ASP.NET路由拍卖多少个U君越L需要时,示例中所示的路由定义发生了下表中列出的结果。

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.

/Controllers/RouteDemo

 译文:上边包车型客车事例呈现了在全局中开创暗中同意MVC路由的代码。asax文件,正如在Visual
Studio项目模板中定义的MVC应用程序。

译文:上边包车型大巴言传身教体现了来自.Globalasax文件的代码,该代码增多了贰个路由对象,该对象定义了多个名字为action和花色称号的UXC60L参数。具备钦赐格局的url被定向到名字为分类.aspx的物理页面。

公海赌船网址 12

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

     (2)  简短;

     (6)  持久、无法改动

6 与路由相关的最首要命名空间和类

公海赌船网址 13

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  

【02】http://www.cnblogs.com/willick/p/3343105.html

译文:下表展现了私下认可的ULacrosseL方式,它彰显了由私下认可路由拍卖的U昂CoraL央求的躬行实践。

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

 Typical URL Patterns in MVC
Applications

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

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

      本深入分析基于1.2
U智跑I、U奥迪Q3L和U奥迪Q7N三者之间的涉嫌图。

假使U科雷傲L不包容在RouteTable集结中定义的任何路由对象,ASP.NET路由不管理诉求。相反,管理被传送给二个ASP.NET
Page ,Web服务,或其余ASP.NET端点。

公海赌船网址 14

译文:您能够透过满含常量来制止格局中的歧义,比方annual/{report}/{year} and monthly/{report}/{year}/{month}。

   
 公海赌船网址 15

1.4 UEvoqueL应怀有特色

   
如UTiguanL:{Controller}/{action}/{id}。当诉求/albums/list/123传来时,该路由就能够尝试相配传入的呼吁,如若相配成功,它就创办三个字典,在那之中饱含了从UPRADOL中分析出的新闻。确切地讲,路由还有大概会向Values字典中为U瑞虎L中的每种路由参数加多八个键;对于价值观路由{Controller}/{action}/{id},Values字典中应有至少含有多少个键,分别是Controller,action和id,如若传入的U宝马X5L是对/albums/list/123的诉求,路由就能够解析该诉求的U瑞鹰L,并为字典的键提供值。本例中,字典中“Controller”键的值为albums,”action”键的值为“list”,”id”键的值是“123”;对于天性路由MVC使用DataTokens字典来存款和储蓄更标准的音讯,并不是操作名称字符串。具体来讲,它饱含三个操作描述符列表,这几个描述符间接指向路由相配时大概利用的操作方法。对于控制器级其他特征路由,列表准将有随处一个操作。在全体MVC中都有使用的RequestContext的RouteData属性保存着外面路由值。

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.

 从内容上来看,天性路由的内容与历史观路由大致,一样有路由U奥迪Q5L、路由值、调控器路由、路由约束和路由私下认可值等。鉴于篇幅限制,在此地就差不离的概述一下,不作详细解说,后期临时间也许有心上人必要,再论述。

译文:带有情势能源的路由axd/pathInfo,被用于幸免Web财富文件的恳求,举个例子WebResource,axd或ScriptResource传递到调控器。对于IIS
7.0,无需其它文件名称扩大。对于IIS
6.0,您必得加多.mvc文件扩大名到URL格局中,如上面的事例:

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

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

(1)路由群集通过RouteBase.GetVirtualPath方法遍历每种路由并访谈:“能够转换给定参数的UENCOREL吗”,那些进度看似于在路由与传播伏乞相称时所运用的至极逻辑。

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

By default, routing does not handle
requests that map to an existing physical file on the Web server. For
example, a request for
http://server/application/Products/Beverages/Coffee.aspx 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}.

  • 谢谢您的读书,若有不足之处,迎接指教,共同学习、共同进步。
  • 博主网站:http://www.cnblogs.com/wangjiming/。
  • 极少一些小说利用读书、参谋、援引、抄袭、复制和粘贴等三种艺术结合而成的,抢先53%为原创。
  • 如你喜欢,麻烦推荐一下;如您有新主见,应接提议,邮箱:2014177728@qq.com。
  • 能够转发该博客,但无法不盛名博客来源。

公海赌船网址 16公海赌船网址 17

公海赌船网址 18

2.12 路由二义性

Index

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.

公海赌船网址 19

 个性路由是在ASP.NET MVC
5中新添的,守旧路由ASP.NET MVC 第11中学就有了。

 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 }

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.

2 路由概述(守旧路由)

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

 
 (2)假设RouteTable中有一个路由成功相称,路由模块就能够从相称成功的路由中收获IRouteHandler接口对象。

 如在VS二〇一一使用MVC模板创制项目时,自动生成类RouteConfig.

 
 路由多个主要任务:相配传入的URubiconL央求和组织与特定路由对应的ULacrosseL,归纳之匹配U中华VL和布局U君越L。在生成UEscortL时,生成UHighlanderL的意思应该首先与接纳用来生成U本田UR-VL的路由相匹配,这样路由就足以在管理传入或传播的U奇骏L时改为完全的双向系统。原则上,开荒人士应该提供一组路由值,以便路由系统从中选中第二个能够相配UCR-VL的路由。

     (4)  能够反映出站点布局;

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

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

公海赌船网址 20公海赌船网址 21

译文:MVC运用程序中的卓越形式

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.

View
Code

id是bikes.

 

译文:假使你想要路由拍卖全体央求,以致指向文件的央求,您能够通过安装RouteCollection对象的属性RouteExistingFiles为true, 
以此来覆盖暗许值。当您将以此值设置为true时,相配定义形式的全数哀告都由路由拍卖。

【07】https://msdn.microsoft.com/en-us/library/dd535620.aspx

 (2)路由体制查看供给的路由参数(即没有提供路由参数的暗中同意值),并保管提供的路由值字典为每八个供给的参数提供贰个值,否则,UEvoqueL生成程序就能立马停下,并回到空值。

【13】https://msdn.microsoft.com/en-us/library/system.web.ui.page.routedata.aspx

【04】Professional Asp.net MVC 5

 
(1)开荒职员调用像Html.ActionLink或Url.Action之类的方法,这几个措施反过来再调用RouteCollection.GetVirtualPath方法,并向它传递一个RequestContext对象、贰个包罗值的字典以及用于采纳生成U哈弗L的路由名称(可选参数)。

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.

 在这一章节,首要深入分析U汉兰达L绑定到调整器操作的最底层细节,深入分析底层原理,驾驭ASP.NET伏乞管道。注意,近日路由不止只是ASP.NET
MVC的贰个表征了,它早就超越那个界定,如ASP.NET Dynamic
Data。路由既不包含MVC的里边知识,也不借助于于MVC。

 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 }

/Controllers/NewFolder1/RouteDemo

 
 使用RouteDebugger调节和测量检验,启用RouteDebugger后,它会用三个DebuggerRouteHandler替换全部路由管理程序,DebugRouteHandler截获全部传入的央求,并查询路由表中的每多少个路由,以便在页面底部呈现路由的确诊数据和参数。为运用RouteDebugger,只需在VS的Package
Manager Console窗口中动用NuGet安装就可以,命令Install-Package
routedebugger.。RouteDebugger包在加多Route
Debugger程序集的还要。也在web.config文件的appSettings节点中增添多个设置,用来开启或禁止使用路由调节和测量检验。

 6.1  路由涉及的命名空间

只在同贰个减轻方案中,存在多少个以及上述同等调整器下的一样action,当U福特ExplorerL央求时,会油可是生二义性。

  • 译文:您为有些路由提供的别的暗中同意值。
  • Any constraints that you have
    provided for a route.

  • 译文:您为路线所提供的另外自律。

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

  • 译文:是不是定义了路由来拍卖与物理文件相配的伏乞。

公海赌船网址 22

译文:除了通过URL中参数的数目来相称ULX570L要求外,还能钦定参数中的值满意一定的自律原则。假如叁个U福特ExplorerL包涵的值超过了路由的约束,那么该路由就不会被用来拍卖央求。您增加了有些羁绊,以保险UEnclaveL参数满含在你的应用程序青海中华南理工科业余大学学学程公司作的值。
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.

 由于篇幅有限,路由章节就写到那,当然,还或然有非常多情节没写,如路由安顿文件、路由平安设置、自定义路由约束等,留给读者朋友们去商讨吗。

假使央求中贫乏三个参数,则动用私下认可值。因而,它们得以产生一条相配您未有预料到的呼吁的路由。比方,假若您增加了以下情势:

设置教程如下:

     
(1)从命名角度,UPRADOI标记财富且独一,UTiggoL标志能源地址
,UCR-VN标记财富名称;

  • 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.
  •  译文:您将它们增加到路由会集的各类。

 
 (1)UrlRoutingModule尝试使用在RouteTable中注册的路由相称当前恳请。

 4.1 高档期的顺序要求的路由管道

其次个重载接受四个参数,当中第一个参数是路由名称。在路由集结中路由名称是独一的,也正是说,未有八个分歧的路由具备同样的名目。当钦点了路由名称时,路由集合就没有要求循环遍历各样路由,相反,它能够即时找到钦赐名称的路由,并移向上边的步骤(2)。假若找到的路由不能够合作内定的参数,该办法就能够回去空值,何况不再相称别的路由。

公海赌船网址 23

8 参谋文献

能够满含为如下流程图:

 公海赌船网址 24

译文:一时你必须管理包含贰个可变多少的ULX570L段的U中华VL需要,在这之中。当您定义八个路由时,您能够钦点,若是三个U凯雷德L的部分比情势中有越多的段,那么额外的段被感觉是最终一部分的一部分。要以这种方法管理额外的段,您能够用星号(*)标识最终三个参数。那被称之为二个不明的参数。一个暗含全部参数的路由也将极其不满含最终叁个参数的任何值的url。上面包车型客车身体力行显示了一个协作未分明的数量的段的路由方式。

 

公海赌船网址 25

A Physical File is Found that Matches
the U君越L Pattern(找到与U普拉多L形式相配的大要文件)

Routing Is Explicitly Disabled for a
U汉兰达L Pattern(为U昂CoraL方式显式禁止使用路由)

【09】https://msdn.microsoft.com/en-us/library/dd329551.aspx

3 路由生成U牧马人L

      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.

2.7 Setting Default Values for U奥迪Q5L
Parameters(为U福特ExplorerL参数设置默许值)

     
 译文:路由是一种被映射到某些管理程序的UAMG ONEL形式。管理程序只怕是贰个物理文件,如在WebForms运用程序中的aspx文件。管理程序也说不定是贰个管理央浼的类,如MVC应用程序中的调节器。要定义路由,您能够因而点名U奇骏L形式、处理程序和路径的名目来创立路由类的实例。

 1.2 UTucsonI、U陆风X8L和UTiguanN三者之间的涉及图

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.

公海赌船网址 26

(2)假诺一个路由能够回答上边的难点(即相配),那么它就能够回来三个满含了UENCOREL的VirTualPathData实例以及别的相配的音信。否则,它就重临空值,路由体制移向列表中的下一个路由。

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.

     
 通常情形下,U库罗德I代表同意能源标记符(Uniform Resource
Identifier)。U中华VI是标记了三个财富的字符串。从才干角度看,全体U安德拉L都以U科雷傲I。W3C以为“U宝马7系L是一个业余的定义,但它特别低价:U福特ExplorerL是UWranglerI的一种档案的次序,它经过代表本身的基本点会见机制来标记能源”,换句话说,URI是某种财富的标记符,而ULX570L则为获取该财富提供了实际的音讯。

译文:约束是经过行使正则表达式或接纳完结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.

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

公海赌船网址 27

 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 }

【11】https://msdn.microsoft.com/en-us/library/system.web.routing.pageroutehandler.aspx

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.

4.2 路由数量

事例引进

4 路由绑定到操作

 【01】https://msdn.microsoft.com/en-us/library/cc668201.aspx\#setting\_default\_values\_for\_url\_parameters

RouteConfig.cs

 3.1 U奥迪Q3L生成的高层系概述

 公海赌船网址 28

2.6 Adding Routes to an MVC
Application

路径2:报告/年,年暗中认可值。

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

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:

 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 }

 
译文:路线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
http://server/application/products/show/bikes 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.

译文:在一些情状下,即便在Web站点启用时,ASP.NET
路由也不管理伏乞。本有的陈诉了一部分现象,在那之中路由不管理央求。

2.4 URL Patterns(URL模式)

1.3 对UKoleosI、U奇骏L和UENVISIONN三者之间剖析

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

译文:暗许意况下,路由不管理映射到Web服务器上存活物理文件的央求。比如,要是存在 Products/Beverages/Coffee.aspx.物理文件,央浼http://server/application/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.

 
路由的主干是七个非常简单的算法,该算法基于三个由RouteCollection类和RouteBase类组成的简易抽象对象。可以运用各样艺术来生成UWranglerL,但这几个格局都是调用RouteCollection.GetVirtual帕特h的一个重载方法而停止。RouteCollection.GetVirtulPath方法公有八个重载版本,上边的代码展现它们的方法签字:

路由2永恒不会管理央浼。第1条只怕是针对性每月报告的,而路由2大概是指向年度报告的。不过,路由第11中学的暗中认可值意味着它将相配任何大概用于路由2的乞求。

 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 }

公海赌船网址 29

        ASP.NET 路由分歧于其余 U卡宴L
重写方案。U汉兰达L 重写通过在将诉求发送到网页在此之前实际更动 UEvoqueL
来管理传入央浼。举个例子,多个应用 UPRADOL 重写的应用程序或许会将 U兰德途锐L 从 /Products/Widgets/ 退换为 /Products.aspx?id=4。其余,U索罗德L 重写经常未有相应的 API
来成立基于格局的 UENVISIONL。在 U中华VL 重写中,如若更动了 UTucsonL
情势,则必得手动更新包涵原始 UENCOREL 的有着超链接。由于
ASP.NET 路由得以从 U福特ExplorerL 提取值,所以拍卖传入乞求时不更换U凯雷德L。借使非得创立一个 UXC90L,则将参数值传递到为你生成 URubiconL
的措施中。若要改造 U途观L
格局,请在某地点变动该方式,您在应用程序中开创的基于该方式的全数链接将自行使用新方式。

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.

1 URI、URL与URN

测验结果

【10】https://msdn.microsoft.com/en-us/library/system.web.routing.route.aspx

译文:上面包车型客车示范显示了怎么选拔MapPageRoute方法创制一条路线,该路由限制了地区和年参数中得以饱含的值。(在MVC应用程序中,您将选取map路由艺术。)

译文:比如,尽管您加多了以下形式:
   Route 1 is set to
{controller}/{action}/{id}

【08】https://msdn.microsoft.com/en-us/library/cc668176.aspx

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.

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.

 1.1 URI、URL和URN定义 

 

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

     (3)  便于输入;

译文:假让你通过成立从调整器基类派生的类来促成调控器的MVC约定,并给它们以“调节器”结尾的称呼,那么您就没有须求在MVC应用程序中手动增添路由了。预配置的路由将调用您在支配器类中贯彻的操作方法。

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.

译文:当路由拍卖UPAJEROL央求时,它尝试将乞请的U帕杰罗L与路由万分。将U翼虎L供给匹配到路由取决于以下原则:

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

2.2  ASP.NET 路由与 URL 重写

     (5)
 应该是“可破解的”,客商能够由此移除UENCOREL的最终,从而达到越来越高档次的新闻体系布局;

 

或许也得以应用NuGet很有益于的设置RouteDebugger,在项目方面右键-“Manage
NuGet Packages”-“Online”输入”RouteDebugger”然后”Install”.

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

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

 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 }

7 总结

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

/ query / { queryname } { * queryvalues
}

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.

     
 注释:财富是二个抽象概念,既可以够指一个文本,也足以指方法调用的结果或服务器上的局地任何内容。

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);
    }
}

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.

        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.

   
调用GetRouteData方法会再次回到RouteData的三个实例。RouteData中包含了关于相称须要的路由消息。

译文:比如,一个带有UOdysseyL路线  /产品
的UPAJEROL映射到二个名称叫ProductsController的调整器。action参数中的值是调用的操作方法的称号。叁个包涵UWranglerL路线  /产品/show
 的UWranglerL会导致对ProductsController类的Showmethod的调用。

译文:您还足以钦点路由不应有管理有些U奇骏L必要。通过定义一条路由,并钦定StopRoutingHandler类应该用于拍卖该模式,进而幸免了拍卖有个别伏乞的路由。当叁个StopRoutingHandler对象管理哀告时,StopRoutingHandler对象会阻塞央浼作为路由的别的附加管理。相反,哀求是用作ASP.NET
页面来管理的,网络页面,Web服务,或别的ASP.NET端点。您能够选择 RouteCollection.Ignore方法。

译文:假使您提供三个字符串,那么路由将字符串作为三个正则表达式来拍卖,并透过调用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.

相关文章