asp.net-mvc – 使用默认控制器的ASP.NET MVC路由
发布时间:2020-05-24 00:00:24 所属栏目:asp.Net 来源:互联网
导读:对于一个场景,我有一个ASP.NET MVC应用程序,其URL包含以下内容: http://example.com/Customer/Listhttp://example.com/Customer/List/Page/2http://example.com/Customer/Listhttp://example.com/Customer/View/8372http://example.co
|
对于一个场景,我有一个ASP.NET MVC应用程序,其URL包含以下内容: http://example.com/Customer/List http://example.com/Customer/List/Page/2 http://example.com/Customer/List http://example.com/Customer/View/8372 http://example.com/Customer/Search/foo/Page/5 这些URL是通过Global.asax.cs中的以下路由实现的 routes.MapRoute(
"CustomerSearch","Customer/Search/{query}/Page/{page}",new { controller = "Customer",action = "Search" }
);
routes.MapRoute(
"CustomerGeneric","Customer/{action}/{id}/Page/{page}",new { controller = "Customer" }
);
//-- Default Route
routes.MapRoute(
"Default","{controller}/{action}/{id}",action = "Index",id = "" }
);
这些都很顺利,直到新的要求到达,并希望将关键字“客户”从网址中删除,以使URL看起来像: http://example.com/List http://example.com/List/Page/2 http://example.com/List http://example.com/View/8372 http://example.com/Search/foo/Page/5 编辑:更正示例链接,感谢@haacked. 我试图添加新的MapRoutes来采取{action},并将默认控制器设置为Customer.例如/ routes.MapRoute(
"CustomerFoo","{action}",action = "Index" }
);
这似乎工作,但是现在Html.ActionLink()生成的所有链接都是奇怪的,不再是URL友好的. 那么这是可以实现的吗?我正走向正确吗? 解决方法不要混合以下规则:“{action} / {id}”与“{controller} / {action} / {id}”的规则…特别是当后面的id具有默认值即可选时.在这种情况下,您没有任何允许路由知道哪一个是正确的路由. 一个解决方法,如果这是你需要的,将是添加一个约束(参见this)到较早的一组值,即列表,视图中的操作.当然,使用这些类型的规则,您不能拥有一个具有相同名称的操作的控制器. 另请记住,如果您指定默认动作& “{action} / {id}”规则中的ID,当您点击您的站点的路由时将使用该规则. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 更改项目URL Visual Studio
- asp.net-mvc – 如何在MVC6或AspNet Core或IdentityCore中更
- ASP.NET应用程序配置IIS后,会出现错误
- asp.net dropdownlist – 在db值之前添加空行
- 在ASP.NET Web服务(ASMX)的JSON请求中,什么时候区分大小写是
- Asp.Net MVC验证 – 依赖字段
- “记住我”与ASP.NET MVC认证不工作
- 使用什么方法将ASP.Net应用程序部署到野外?
- asp.net-mvc – 路由:如何在url中隐藏动作名称?
- asp.net – SignalR 3的开发状况如何?
推荐文章
站长推荐
- asp.net – MVC标识||禁用所有cookie并“记住我”
- asp.net – LINQ to SQL – 你的DataContext在哪
- asp.net-mvc – 使用一组复杂数据类型调用Update
- asp.net-mvc-4 – ASP.NET MVC 4应用程序捆绑和分
- 在ASP.NET中下载文件的实现代码
- asp.net-mvc – ASP.NET MVC内联Razor变量
- asp.net – 在使用预编译的Razor视图和VirtualPa
- asp.net-mvc – Asp.net MVC 3 RC – Razor Vali
- asp.net-mvc – 如何将XML作为POST传递给ASP MVC
- 何时使用ASP.NET MVC与ASP.NET Web窗体?
热点阅读
