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去解決的。
——————————————————————————————————————————————————————————————————————————————