1

I have website in asp.net web forms. It uses url friendly structure. suppose I have url www.site.com/experience/experience-category. Here there are two different pages experience & experience-category. Now whenever I try to access this url www.site.com/experience/experience-category it doeson't shows me this page. It showing me www.site.com/experience page. How this can be resolved?

RouteConfig

Public Module RouteConfig
    Public Sub RegisterRoutes(routes As RouteCollection)
        Dim settings = New FriendlyUrlSettings()
        settings.AutoRedirectMode = RedirectMode.Permanent
        routes.EnableFriendlyUrls(settings)

        routes.MapPageRoute("experience-category", "experience/{name}", "~/experience-category.aspx") 'For Experience Category
    End Sub
End Module
SUN
  • 973
  • 14
  • 38

1 Answers1

1

There's a number of things you can do.

What routes.EnableFriendlyUrls(settings) basically does is adding 2 Routes to your route collection.

Your problem is that you have an .aspx (experience.aspx) with the same name as part of the route url you set up ( experience/{name} ). The routing will look for the first match in your route collection (in this case it'll be your EnableFriendlyUrls routes).

If you want to overcome this you can do the following:

1.Execute routes.MapPageRoute before routes.EnableFriendlyUrls(settings) :

    Public Module RouteConfig
    Public Sub RegisterRoutes(routes As RouteCollection)
        routes.MapPageRoute("experience-category", "experience/{name}", "~/experience-category.aspx") 'For Experience Category

        Dim settings = New FriendlyUrlSettings()
        settings.AutoRedirectMode = RedirectMode.Permanent
        routes.EnableFriendlyUrls(settings)

    End Sub
End Module

This will put this rule first, beating the FriendlyUrlSettings:

Or

2. Use insert to insert your rule at the start of the routes collection (instead of MapPageRoute):

    Dim settings = New FriendlyUrlSettings()
    settings.AutoRedirectMode = RedirectMode.Permanent
    routes.EnableFriendlyUrls(settings)

    Dim overwrExperienceUrl As String = "experience/{name}"
    Dim overwrExperiencePRH As New PageRouteHandler("~/experience-category.aspx")
    Dim overwrExperienceRoute As New Route(overwrExperienceUrl, overwrExperiencePRH)
    routes.Insert(0, overwrExperienceRoute)

3.Avoid a match with the EnableFriendlyUrl routing rules:

Move your control(s) (Experience.aspx) to a folder so it won't interfere in this case.

vbnet3d
  • 1,151
  • 1
  • 19
  • 37