ASP.NET 4路由问题

ASP.NET 4路由问题

问题描述:

我试图做我的Global.asax文件如下:ASP.NET 4路由问题

目前,我必须确定我的路线是这样的:

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

注意这个词的网页前{slug}

现在,如果我把它定义是这样的:

​​

它确实ñ工作。

我的CSS和JS文件不会加载,我得到一个404

但是,如果我这样做:

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

它工作正常?

基本上我想要我的网址看起来像这样:

example.com/contactexample.com/about-us,它是所有基于该{}塞数据库动态提供。

任何人都可以帮忙吗?

使用:

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

工作正常,我。你需要确保你的路线是正确的顺序;具体到一般,但也有一个资源等忽略之一,否则他们也会被路由到那里。

希望帮助

编辑

忽略像路线:

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

+1 - 解决方案与http://www.stevefenton.co.uk/Content/Blog/Date/201101/Blog/The-Nicest-URLs-In-ASP-NET-MVC/类似 – Fenton 2011-01-27 10:47:11

How to ignore route in asp.net forms url routing

也许这样的事情,虽然我不能在此刻进行测试。根据我的理解,它应该告诉路由处理程序忽略这些目录中的任何内容。

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

谢谢你们!我不得不重新订购我的路线。

我使用HttpHandler来组合和gzip我的js和css文件。这是正在最后添加像这样:

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

我切换这些身边:

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

我加了StopRoutingHandler的WebResource.axd的,现在这一切工作漂亮!