Search code examples
c#asp.netasp.net-routing

ASP.NET 4 routing question


I am trying to do the following in my Global.asax file:

At the moment i have to define my route like this:

routes.MapPageRoute(
    "ViewPage", 
    "pages/{slug}",
    "~/viewpage.aspx",
    false
);

Notice the word pages before the {slug}

Now if i define it like this:

routes.MapPageRoute (
    "ViewPage", 
    "{slug}",
    "~/viewpage.aspx",
    false
);

It does not work.

My CSS and JS files wont load, i get a 404.

But, if i do this:

routes.MapPageRoute (
    "ContactPage", 
    "contact",
    "~/contact.aspx",
    false
);

It works fine??

Basically i want my urls to look like this:

example.com/contact or example.com/about-us and it is all served dynamically from the database based on the {slug}.

Can anyone help?


Solution

  • Using:

    RouteTable.Routes.MapPageRoute("slug", 
                    "{slug}",
                    "~/page.aspx", false);
    

    Works fine for me. What you need to make sure is that your routes are in the right order; specific to general but also have an ignore one for resources etc. otherwise they'll be routed there too.

    Hope that helps

    Edit

    Ignore routes like:

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