MVC 路由 - 避免同一位置使用多个 URL
ASP.net MVC 中的默认路由如下:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
这意味着我可以通过多种方式访问 HomeController / Index 操作方法:
http://localhost/home/index
http://localhost/home/
http://localhost/
如何避免同一操作使用三个 URL?
The default route in ASP.net MVC is the following:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This means that I can reach the HomeController / Index action method in multiple ways:
http://localhost/home/index
http://localhost/home/
http://localhost/
How can I avoid having three URL's for the same action?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想:
那么:
如果您只想:
那么:
如果您只想:
那么:
If you want only:
then:
If you want only:
then:
and if you want only:
then:
您会看到默认值开始生效。
摆脱控制器和操作的默认值。
这将使用户在控制器中键入:行动。
You are seeing the default values kick in.
Get rid of the default values for controller and action.
This will make a user type in the controller & action.
我不认为你这样做是为了 SEO?谷歌会因为重复的内容而惩罚你,所以这是值得思考的。
如果是这种情况,路由并不是解决问题的最佳方法。您应该在主页的
中添加规范链接。因此,请将
放在 Views/Home/Index.aspx 页面以及任何 url 搜索的头部引擎访问您的主页,所有 SEO 价值将归因于规范链接中引用的 url。
详细信息:关于规范标记
我去年写了一篇文章关于从开发者角度考虑的 SEO 问题如果你也在看这类东西
I don't suppose you are doing this for SEO? Google penalises you for duplicate content so it is worthy of some thought.
If this is the case routing is not the best way to approach your problem. You should add a canonical link in the
<head>
of your homepage. So put<link href="http://www.mysite.com" ref="canonical" />
in the head of your Views/Home/Index.aspx page and whatever url search engines access your homepage from, all SEO value will be attributed to the url referenced in the canonical link.More info: about the canonical tag
I wrote an article last year about SEO concerns from a developer perspective too if you are looking at this kind of stuff