asp.net-mvc – 将索引设置为控制器的默认路由
发布时间:2020-12-16 04:19:33 所属栏目:asp.Net 来源:网络整理
导读:我有一个网址 http://www.roadkillwiki.org/Page/Index/documentation 我想变成 http://www.roadkillwiki.org/Page/documentation 这也可能像http://www.roadkillwiki.org/Page/my-url-with-spaces – 参数是一个字符串.我试过的路线设置是: routes.MapRout
我有一个网址
> http://www.roadkillwiki.org/Page/Index/documentation 我想变成 > http://www.roadkillwiki.org/Page/documentation 这也可能像http://www.roadkillwiki.org/Page/my-url-with-spaces – 参数是一个字符串.我试过的路线设置是: routes.MapRoute( "ControllerDefault","{controller}/{id}",new { controller = "Page",action = "Index",id = UrlParameter.Optional } ); 然而,这干扰了MVC项目带来的默认“id”路由.有没有办法实现这个目标? 解决方法
您不需要丢失默认路由.避免路线相互干扰的关键是对它们进行排序,以便更具体的规则先于不太具体的规则.例如:
// Your specialized route routes.MapRoute( "Page","Page/{slug}",action = "Index" } ); // Default MVC route (fallback) routes.MapRoute( "Default",// Route name "{controller}/{action}/{id}",// URL with parameters new { controller = "Home",id = UrlParameter.Optional } // Parameter defaults ); 然后你的PageController看起来像这样: using System.Web.Mvc; public class PageController : Controller { public string Index(string slug) { // find page by slug } } 也就是说,我强烈建议你这样做: // Your specialized route routes.MapRoute( "Page","Page/{id}/{slug}",slug = UrlParameter.Optional } ); // MVC's default route (fallback) routes.MapRoute( "Default",id = UrlParameter.Optional } // Parameter defaults ); 而你的PageController: using System.Web.Mvc; public class PageController : Controller { public string Index(int id) { // find page by ID } } 通过在URL的开头(如StackOverflow)或最后包含页面ID,您可以忽略slug,而是通过ID检索页面.如果您的用户更改页面名称,这将为您节省大量的麻烦.我经历过这一切,这很痛苦;你基本上必须记录你的页面过去所有的名字,这样你的访问者/搜索引擎每次重命名页面都不会得到404. 希望这可以帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 什么是ct100,如何重命名?
- ASP.NET绑定与EVAL
- .net – 设置可用于AppFabric缓存的内存量
- asp.net – 当DataSource中没有数据时,DetailsView不可见
- asp.net-mvc – Web Essentials浏览器链接在Visual Studio
- asp.net – 将Visual Studio本地IIS 7调试虚拟路径设置为/
- 比较一下以“反射”和“表达式”执行方法的性能差异
- asp-classic – VBScript条件短路解决方法
- asp.net-core – 如何在Azure Web Apps中将redirect_uri协议
- asp.net-mvc-3 – 为什么抛出NULL值异常?
推荐文章
站长推荐
- asp.net-mvc – aspnet_Profiles表中的PropertyV
- asp.net-mvc – 当参数为Model时,ASP.NET MVC发布
- 在ASP.NET Core应用程序中使用标准Active Direct
- .net – 多选列表框中的预选项目(MVC3 Razor)
- .Net 5分钟搞定网页实时监控
- asp.net – TextBox在使用TextMode Number回发后
- 最佳实践:ASP.NET中的CSS或主题?
- asp.net-mvc – ASP.NET MVC V2 – 好友类
- asp.net – ASPXGridView ClientSideEvents如何获
- asp.net – SmtpClient.SendAsync调用自动取消
热点阅读