ASP.net Web forms - Page Routing Naming Conflict

236 Views Asked by At

I'm rebuilding an old project from asp 4.0 to a 4.52 (moving to bootstrap). This is a Web Forms website not MVC. My original version was made with VS2012 and now it's with VS 2015 which chagned a bit for example adding by default the RouteConfig class.

In V4.0 my url formation used to be as follows:

domain.com/nike-shoes

domain.com/nike-shoes/nike-air-pegasus-21-1001

In my new site it does not work because of the "/nike-shoes" duplicity.

Calling "domain.com/nike-shoes/nike-air-pegasus-21" will simply call "domain.com/nike-shoes"

Here is the "new" code:

Sub Application_Start(sender As Object, e As EventArgs)
    RouteConfig.RegisterRoutes(RouteTable.Routes)
    BundleConfig.RegisterBundles(BundleTable.Bundles)
    RegisterRoutes(RouteTable.Routes)
End Sub

Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.MapPageRoute("Nike-Shoes", "nike-shoes", "~/nike-shoes.aspx")
    routes.MapPageRoute("Nike-Shoe", "nike-shoes/{shoe-id}", "~/display-shoe.aspx")
End Sub

The old code RegisterRoutes Sub was the same but the Application_Start was different (there was no RouteConfig):`

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)            
        RegisterRoutes(RouteTable.Routes)
    End Sub

Previous version also needed re-routing from /page.aspx to /page which now is by default. Since I do not want to do redirects keeping current url formation is extremely important.

THANKS a lot!

2

There are 2 best solutions below

1
iorien On

I think you need to have different names for every route name, something like:

Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.MapPageRoute("Nike-Shoes", "nike-shoes", "~/nike-shoes.aspx")
    routes.MapPageRoute("Nike-Shoes-2", "nike-shoes/{shoe-id}", "~/display-shoe.aspx")
End Sub
0
user1436942 On

Problem solved -

Changed:

RouteConfig.RegisterRoutes(RouteTable.Routes)
BundleConfig.RegisterBundles(BundleTable.Bundles)
RegisterRoutes(RouteTable.Routes)

To:

 RegisterRoutes(RouteTable.Routes)
    RouteConfig.RegisterRoutes(RouteTable.Routes)
    BundleConfig.RegisterBundles(BundleTable.Bundles)

Thanks