【ASP.NET】System.Web.Routing - RouteCollection Class

Provides a collection of routes for ASP.NET routing.

The RouteCollection class provides methods that enable you to manage a collection of objects that derive from the RouteBase class.

这个类相对而言比较复杂了。支持很多方法和属性,以及扩展方法。下面会记录一些我用过的方法。

 

在asp.net app中, 启动文件一般会写成这样:

这里我们看到红圈中标识的就是route collection。

    /// <summary>Ignores the specified URL route for the given list of available routes.</summary>
    /// <param name="routes">A collection of routes for the application.</param>
    /// <param name="url">The URL pattern for the route to ignore.</param>
    /// <exception cref="T:System.ArgumentNullException">The <paramref name="routes" /> or <paramref name="url" /> parameter is null.</exception>
    public static void IgnoreRoute(this RouteCollection routes, string url);

    /// <summary>Maps the specified URL route.</summary>
    /// <returns>A reference to the mapped route.</returns>
    /// <param name="routes">A collection of routes for the application.</param>
    /// <param name="name">The name of the route to map.</param>
    /// <param name="url">The URL pattern for the route.</param>
    /// <exception cref="T:System.ArgumentNullException">The <paramref name="routes" /> or <paramref name="url" /> parameter is null.</exception>
    public static Route MapRoute(this RouteCollection routes, string name, string url);

 MapRoute是最为常用的一种配置route的方式, 它同时有多中重载形式可以根据不同的情况进行使用。

    /// <summary>Provides a way to define routes for Web Forms applications.</summary>
    /// <returns>The route that is added to the route collection.</returns>
    /// <param name="routeName">The name of the route.</param>
    /// <param name="routeUrl">The URL pattern for the route.</param>
    /// <param name="physicalFile">The physical URL for the route.</param>
    /// <param name="checkPhysicalUrlAccess">A value that indicates whether ASP.NET should validate that the user has authority to access the physical URL (the route URL is always checked). This parameter sets the <see cref="P:System.Web.Routing.PageRouteHandler.CheckPhysicalUrlAccess" /> property.</param>
    /// <param name="defaults">Default values for the route.</param>
    /// <param name="constraints">Constraints that a URL request must meet in order to be processed as this route.</param>
    public Route MapPageRoute(string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess, RouteValueDictionary defaults, RouteValueDictionary constraints);

 MapPageRoute据说是asp.net web form中的route方式,但是也有和MVC混合使用的。这种MapRoute以及MapPageRoute混合使用会造成route conflict, 导致app中使用的Html.Action这种寻找Action的方式失效。效果就是actiion本身访问的action的URL却指向到MapPageRoute指定的URL当中去。 该问题目前我是通过调整route注册顺序以及加入一些约束, 比如Regex去解决的。

 

 

——————————————————————————————————————————————————————————————————————————————

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.web.routing.routecollection?view=netframework-4.7.2

posted @ 2018-11-06 14:42  YanyuWu  阅读(258)  评论(0编辑  收藏  举报