HTML5技术

【ASP.NET MVC 牛刀小试】 ASP.NET MVC 路由 - Alan_beijing(6)

字号+ 作者:H5之家 来源:H5之家 2017-06-29 11:01 我要评论( )

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 shoul

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  

译文:您还可以指定路由不应该处理某些URL请求。通过定义一条路由,并指定StopRoutingHandler类应该用于处理该模式,从而避免了处理某些请求的路由。当一个StopRoutingHandler对象处理请求时,StopRoutingHandler对象会阻塞请求作为路由的任何附加处理。相反,请求是作为ASP.NET 页面来处理的,网络页面,Web服务,或其他ASP.NET端点。您可以使用 RouteCollection.Ignore方法。

RegisterRoutes(RouteCollection routes) 2 { ); 4 }

2.11 How URLs Are Matched to Routes(url如何与路由匹配)

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:

译文:当路由处理URL请求时,它尝试将请求的URL与路由匹配。将URL请求匹配到路由取决于以下条件:

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

  • Any default values that you have provided for a route.
  • 译文:您为某个路由提供的任何默认值。
  • Any constraints that you have provided for a route.

  • 译文:您为路线所提供的任何约束。
  • Whether you have defined routing to handle requests that match a physical file.

  • 译文:是否定义了路由来处理与物理文件匹配的请求。
  • For example, suppose that you add routes with the following patterns:

    译文:例如,假设您添加了以下模式:
       Route 1 is set to {controller}/{action}/{id}

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

      译文:路线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.

    译文:

    路由2永远不会处理请求,因为路由1首先被匹配,它总是匹配可能在2号路径上工作的请求。请求似乎比赛路线2更紧密,但它是由路线1以下值:

    控制器products.

    行动是show.

    id是bikes.

    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.

    译文:

    如果请求中缺少一个参数,则使用默认值。因此,它们可以导致一条匹配您没有预料到的请求的路由。例如,假设您添加了以下模式:

    路线1:报告/年/月,年和月默认值。

    路线2:报告/年,年默认值。

    路由2永远不会处理请求。第1条可能是针对每月报告的,而路由2可能是针对年度报告的。但是,路由1中的默认值意味着它将匹配任何可能用于路由2的请求。

    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.

    译文:您可以通过包含常量来避免模式中的歧义,例如 annual/{report}/{year} and monthly/{report}/{year}/{month}。

    如果URL不匹配在RouteTable集合中定义的任何路由对象,ASP.NET路由不处理请求。相反,处理被传递给一个ASP.NET Page ,Web服务,或其他ASP.NET端点。

    2.12 路由二义性

    只在同一个解决方案中,存在两个以及以上相同控制器下的相同action,当URL请求时,会出现二义性。

    二义性Demo目录结构

    RouteConfig.cs

     

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • Asp.Net WebForm生命周期的详解 - 天使不哭

      Asp.Net WebForm生命周期的详解 - 天使不哭

      2017-06-15 09:00

    • 关于ASP.NET WebForm与ASP.NET MVC的比较 - 天使不哭

      关于ASP.NET WebForm与ASP.NET MVC的比较 - 天使不哭

      2017-06-09 09:02

    • [asp.net mvc 奇淫巧技] 04 - 你真的会用Action的模型绑定吗? - Emrys5

      [asp.net mvc 奇淫巧技] 04 - 你真的会用Action的模型绑定吗? - Emr

      2017-06-02 13:00

    • Amazing ASP.NET Core 2.0 - Savorboard

      Amazing ASP.NET Core 2.0 - Savorboard

      2017-05-25 14:00

    网友点评
    p