Search code examples
c#asp.netroutesurl-routingasp.net-routing

Routing multiple pages in ASP .net c#


I am trying to route 2 pages but its only routing the first registered one only. Here is the code:

 static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("DynamicCity", "{dCity}.aspx", "~/DynamicCity.aspx");
    routes.MapPageRoute("DynamicPage", "{Description}.aspx", "~/DynamicPage.aspx");
}

Why its only routing DynamicCity?


Solution

  • I am not sure if this will help you because you didn't specify what your URL scheme is. But if all you want is to send /DynamicCity.aspx to the ~/DynamicCity.aspx page and have all of your other pages routed to their own names, you could just do something like this:

    static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("Page", "{pageName}.aspx", "~/{pageName}.aspx");
    }
    

    But then, since ASP.NET will do this anyway, I am not sure what you are expecting from .NET routing.

    Normally, people use MapPageRoute to remove the .aspx extension from the URL (like shown) or to completely change the URL.

    static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("Page", "{pageName}", "~/{pageName}.aspx");
    }
    

    Have a look at this answer for some other ideas.