2012-04-19 192 views
1

我有這樣的MapPageRoute在我的Global.asax .asp頁Asp.Net 4.0:忽略路由的路由

RouteTable.Routes.MapPageRoute("TestPages", "{file}", "~/Test/{file}"); 

它基本上是說,如果請求來自於它進入test文件夾中的任何文件。不過,我想限制它,只有在URL中沒有asp擴展時,纔會執行此規則。所以,如果用戶輸入Test.asp,則不會發生URL路由。但是如果它像http://www.something.com/Test/這樣的路線應該執行。

我該如何做到這一點?

+0

嘗試將此作爲定義中的第一條路線。 routes.Ignore( 「{}資源的.asp」); – 2012-04-19 12:36:23

回答

2

要忽略的路線嘗試使用以下:

RouteTable.Routes.Ignore("{resource}.asp/{*pathInfo}"); 

改變 '的.asp'到您想要過濾的類型。

2

我爲此使用了一個約束。示例:

routes.MapPageRoute("CMS", "{*friendlyUrl}", "~/index.aspx", true, null, new RouteValueDictionary { { "incomingUrl", new CatchallConstraint() } }); 

其中CatchallContraint是必須實現IRouteConstraint的類。

在Match方法中,只需檢查文件擴展名,如果它是一個asp擴展名,則返回false。

這裏是我的實現(vb.net) - 它比你需要的多一點,因爲它可以在web.config中配置,但你明白了。

公共類CatchallConstraint 實現System.Web.Routing.IRouteConstraint

''' <summary> 
''' If AppSettings: CatchallIgnoredExtensions doesn't exist, these are the default extensions to ignore in the catch-all route 
''' </summary> 
''' <remarks></remarks> 
Public Const DefaultIgnoredExtensions As String = ".jpg,.gif,.png" 

''' <summary> 
''' For the catch-all route, checks the AppSettings: CatchallIgnoredExtensions to determine if the route should be ignored. 
''' Generally this is for images - if we got to here that means the image was not found, and there's no need to follow this route 
''' </summary> 
Public Function Match(ByVal httpContext As System.Web.HttpContextBase, ByVal route As System.Web.Routing.Route, ByVal parameterName As String, ByVal values As System.Web.Routing.RouteValueDictionary, ByVal routeDirection As System.Web.Routing.RouteDirection) As Boolean Implements System.Web.Routing.IRouteConstraint.Match 
    If routeDirection = Routing.RouteDirection.IncomingRequest Then 
     Dim friendlyUrl As String = Nothing 
     If values.TryGetValue("friendlyUrl", friendlyUrl) AndAlso Not String.IsNullOrEmpty(friendlyUrl) Then 
      If friendlyUrl.Contains(".") Then 
       Dim catchallIgnoredExtensions As String = ConfigurationManager.AppSettings("CatchallIgnoredExtensions") 
       ' only set defaults if the setting is not found - user may not want to ignore any extensions 
       If catchallIgnoredExtensions Is Nothing Then 
        catchallIgnoredExtensions = DefaultIgnoredExtensions 
       End If 
       ' replace spaces and period to standardize, surround the extensions in commas for searching 
       catchallIgnoredExtensions = "," & catchallIgnoredExtensions.Replace(" ", "").Replace(".", "").ToLowerInvariant() & "," 
       Dim extension As String = System.IO.Path.GetExtension(friendlyUrl).Replace(".", "") 
       If catchallIgnoredExtensions.Contains("," & extension & ",") Then 
        Return False 
       End If 
      End If 
     End If 
    End If 
    Return True 
End Function 

末級

+0

謝謝。但Psycho的解決方案要好得多。 – Jack 2012-04-19 14:01:09

+0

@TomKaufmann他的解決方案不適用於超出根目錄的.asp擴展名。 – ScottE 2012-04-19 14:29:00

+0

實際上,我只有根目錄下的asp頁面。 – Jack 2012-04-20 07:38:35