【问题标题】:Ignoring routing for .asp pages Asp.Net 4.0 routing忽略 .asp 页面的路由 Asp.Net 4.0 路由
【发布时间】:2012-04-30 23:02:53
【问题描述】:

我的 global.asax 中有这个 MapPageRoute:

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

它基本上说如果请求来自任何文件,它会进入测试文件夹。但是我想限制它,以便仅当 URL 中不存在 asp 扩展名时才执行此规则。因此,如果用户键入 Test.asp,则不应发生 URL 路由。但是如果它像http://www.something.com/Test/ 这样的路由应该被执行。

我怎样才能做到这一点?

【问题讨论】:

  • 尝试将此作为定义中的第一条路线。 routes.Ignore("{resource}.asp");

标签: c# asp.net routing url-routing asp.net-4.0


【解决方案1】:

要忽略路由,请尝试使用以下命令:

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
    

    结束类

    【讨论】:

    • 谢谢。但是 Psycho 的解决方案要好得多。
    • @TomKaufmann 他的解决方案不适用于根目录以外的 .asp 扩展。
    • 实际上我的asp页面只在root中。
    猜你喜欢
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2010-09-21
    • 2011-07-30
    相关资源
    最近更新 更多