1

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?

3 Answers 3

3

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}");
Sign up to request clarification or add additional context in comments.

1 Comment

1

How to ignore route in asp.net forms url routing

Maybe something like this, although I cannot test it at the moment. From what I understand it should tell the routing handler to ignore anything in those directories.

routes.Add(new Route("images/", new StopRoutingHandler()));
routes.Add(new Route("js/", new StopRoutingHandler()));
routes.Add(new Route("css/", new StopRoutingHandler()));

Comments

1

Thanks guys!!

I had to re-order my routes.

I use a HttpHandler to combine and gzip my js and css files. This was being added last like so:

const string combine = "~/code/httphandlers/httpcombiner.ashx";
RegisterRoutes(RouteTable.Routes);
RouteTable.Routes.Add(new Route("combine", new HttpHandlerRoute(combine)));

I switched these around to:

const string combine = "~/code/httphandlers/httpcombiner.ashx";
RouteTable.Routes.Add(new Route("combine", new HttpHandlerRoute(combine)));
RegisterRoutes(RouteTable.Routes);

I added the StopRoutingHandler for the webresource.axd and now it all works beautiful!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.