1

I have this MapPageRoute in my global.asax:

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

It basically says if request comes for any file it goes to Test folder. However I want to restrict it such that this rule only gets executed when asp extension is not there in URL. So, if user types Test.asp, no URL routing should happen. But if it something like http://www.something.com/Test/ this route should be executed.

How can I achieve that?

1
  • try this as first route in definition. routes.Ignore("{resource}.asp"); Apr 19, 2012 at 12:36

2 Answers 2

2

To ignore a route try using the following:

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

Change '.asp' to the type you want to filter.

2

I use a constraint for this purpose. Example:

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

Where CatchallContraint is a class that must implement IRouteConstraint.

In the Match method, just check for the file extension, and return false if it's an asp extension.

Here's my implementation (vb.net) - it's a bit more than you need as it's configurable in the web.config, but you get the idea.

Public Class CatchallConstraint Implements 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

End Class

3
  • Thanks. But Psycho's solution is much better.
    – Jack
    Apr 19, 2012 at 14:01
  • @TomKaufmann His solution won't work for .asp extensions beyond the root directory.
    – ScottE
    Apr 19, 2012 at 14:29
  • actually I have my asp pages in root only.
    – Jack
    Apr 20, 2012 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.