[英]ASP.NET 4 routing question
I am trying to do the following in my Global.asax file: 我正在尝试在Global.asax文件中执行以下操作:
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}
注意
{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. 我的CSS和JS文件无法加载,我得到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}. example.com/contact
或example.com/about-us
,所有这些都基于{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 如何忽略asp.net形式的URL路由中的路由
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. 我使用HttpHandler来组合和gzip我的js和css文件。 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! 我为webresource.axd添加了StopRoutingHandler,现在一切正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.