开发者

ASP.NET 4 routing question

开发者 https://www.devze.com 2023-02-07 04:27 出处:网络
I am trying to do the following in my Global.asax file: At the moment i have to define my route like this:

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",
  开发者_C百科  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?


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}");


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()));


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消