RouteCollection.Ignore 方法
定义
定义不需检查是否匹配路由的 URL 模式。Defines a URL pattern that should not be checked for matches against routes.
重载
| Ignore(String, Object) |
定义一个 URL 模式,此模式在请求 URL 满足指定约束的情况下不需要检查 URL 是否与路由匹配。Defines a URL pattern that should not be checked for matches against routes if a request URL meets the specified constraints. |
| Ignore(String) |
定义不需检查是否匹配路由的 URL 模式。Defines a URL pattern that should not be checked for matches against routes. |
注解
此方法创建映射到 StopRoutingHandler 路由处理程序的路由。This method creates a route that is mapped to the StopRoutingHandler route handler. 调用此方法后,匹配指定 URL 模式的请求将不会作为路由请求处理。After you call this method, requests that match the specified URL pattern will not be processed as route requests.
当 URL 与物理文件(如图像文件)匹配时,ASP.NET 路由会自动忽略请求。ASP.NET routing automatically ignores requests when the URL matches a physical file, such as an image file. 在某些情况下,你可能还希望路由在没有物理文件时忽略请求。In some cases you might also want routing to ignore requests when there is no physical file. 例如,即使没有与 axd 文件扩展名相对应的物理文件,也不应将 ASP.NET 自动对 axd 文件进行的请求视为路由请求。For example, the requests that ASP.NET automatically makes for .axd files should not be treated as route requests even though there is no physical file that corresponds to the .axd file-name extension.
Ignore(String, Object)
定义一个 URL 模式,此模式在请求 URL 满足指定约束的情况下不需要检查 URL 是否与路由匹配。Defines a URL pattern that should not be checked for matches against routes if a request URL meets the specified constraints.
public:
void Ignore(System::String ^ url, System::Object ^ constraints);
public void Ignore (string url, object constraints);
member this.Ignore : string * obj -> unit
Public Sub Ignore (url As String, constraints As Object)
参数
- url
- String
要忽略的 URL 模式。The URL pattern to be ignored.
- constraints
- Object
附加条件,用于确定是否忽略匹配 URL 模式的请求。Additional criteria that determine whether a request that matches the URL pattern will be ignored.
例外
url 参数为 null。The url parameter is null.
示例
下面的示例演示如何使用此方法忽略扩展名为 .aspx 的所有 Url。The following example shows how to use this method to ignore all URLs that have an .aspx extension. 如果注册自定义 HTTP 处理程序来处理扩展名为 ".aspx" 的文件的所有 Url,则可能需要执行此操作。You might want to do this if you register a custom HTTP handler to handle all URLs for files that have the extension ".aspx". 匹配所有 .aspx 请求的单一 URL 模式需要两个通用参数,例如 {*path}.aspx/{*pathinfo} 。A single URL pattern that would match all .aspx requests would require two catchall parameters such as {*path}.aspx/{*pathinfo}. (此模式将匹配以 .aspx 结尾的任何 URL,其中包括具有查询字符串参数的 URL。 ) 不过,路由在末尾只允许使用一个参数。(This pattern would match any URL that ends in .aspx, including those that have query-string parameters.) However, routing allows only one catchall parameter at the end. 作为替代方法,您可以指定一个 URL 模式,该 URL 模式具有与所有 Url 匹配的单个内参数,然后指定排除不具有 .aspx 扩展名的所有内容的约束,如以下示例中所示:As an alternative, you can specify a URL pattern that has a single catchall parameter that matches all URLs and then specify constraints that exclude everything that does not have the .aspx extension, as shown in the following example:
routes.Ignore("{*allaspx}", new With {.allaspx = ".*\.aspx(/.*)?"})
routes.Ignore("{*allaspx}", new {allaspx=@".*\.aspx(/.*)?"});
之前的代码行将通常添加到从 global.asax 中的方法调用的方法中 Application_Start ,如重载的示例中所示 Ignore(String) 。The previous line of code would typically be added to a method that is called from the Application_Start method in the Global.asax, as shown in the example for the Ignore(String) overload.
注解
此方法创建映射到 StopRoutingHandler 路由处理程序的路由。This method creates a route that is mapped to the StopRoutingHandler route handler. 调用此方法后,匹配指定 URL 模式的请求将不会作为路由请求处理。After you call this method, requests that match the specified URL pattern will not be processed as route requests.
当 URL 与物理文件(如图像文件)匹配时,ASP.NET 路由会自动忽略请求。ASP.NET routing automatically ignores requests when the URL matches a physical file, such as an image file. 在某些情况下,你可能还希望路由在没有物理文件时忽略请求。In some cases you might also want routing to ignore requests when there is no physical file. 例如,即使没有与 axd 文件扩展名相对应的物理文件,也不应将 ASP.NET 自动对 axd 文件进行的请求视为路由请求。For example, the requests that ASP.NET automatically makes for .axd files should not be treated as route requests even though there is no physical file that corresponds to the .axd file-name extension.
适用于
Ignore(String)
定义不需检查是否匹配路由的 URL 模式。Defines a URL pattern that should not be checked for matches against routes.
public:
void Ignore(System::String ^ url);
public void Ignore (string url);
member this.Ignore : string -> unit
Public Sub Ignore (url As String)
参数
- url
- String
要忽略的 URL 模式。The URL pattern to be ignored.
示例
MVC 项目的默认模板使用此方法从路由中排除 axd 文件,如以下示例中所示:The default template for MVC projects uses this method to exclude .axd files from routing, as shown in the following example:
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);
}
}
Public Class MvcApplication
Inherits System.Web.HttpApplication
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
' MapRoute takes the following parameters, in order:
' (1) Route name
' (2) URL with parameters
' (3) Parameter defaults
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = ""} _
)
End Sub
Sub Application_Start()
RegisterRoutes(RouteTable.Routes)
End Sub
End Class
注解
此方法创建映射到 StopRoutingHandler 路由处理程序的路由。This method creates a route that is mapped to the StopRoutingHandler route handler. 调用此方法后,匹配指定 URL 模式的请求将不会作为路由请求处理。After you call this method, requests that match the specified URL pattern will not be processed as route requests.
当 URL 与物理文件(如图像文件)匹配时,ASP.NET 路由会自动忽略请求。ASP.NET routing automatically ignores requests when the URL matches a physical file, such as an image file. 在某些情况下,你可能还希望路由在没有物理文件时忽略请求。In some cases you might also want routing to ignore requests when there is no physical file. 例如,即使没有与 axd 文件扩展名相对应的物理文件,也不应将 ASP.NET 自动对 axd 文件进行的请求视为路由请求。For example, the requests that ASP.NET automatically makes for .axd files should not be treated as route requests even though there is no physical file that corresponds to the .axd file-name extension.