MVC 路由 - 避免同一位置使用多个 URL

发布于 2024-10-20 04:31:51 字数 414 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吝吻 2024-10-27 04:31:51

如果您只想:

http://localhost/

那么:

routes.MapRoute(
    "Default",
    "",
    new { controller = "Home", action = "Index" }
);

如果您只想:

http://localhost/home/

那么:

routes.MapRoute(
    "Default",
    "home",
    new { controller = "Home", action = "Index" }
);

如果您只想:

http://localhost/home/index

那么:

routes.MapRoute(
    "Default",
    "home/index",
    new { controller = "Home", action = "Index" }
);

If you want only:

http://localhost/

then:

routes.MapRoute(
    "Default",
    "",
    new { controller = "Home", action = "Index" }
);

If you want only:

http://localhost/home/

then:

routes.MapRoute(
    "Default",
    "home",
    new { controller = "Home", action = "Index" }
);

and if you want only:

http://localhost/home/index

then:

routes.MapRoute(
    "Default",
    "home/index",
    new { controller = "Home", action = "Index" }
);
逆光飞翔i 2024-10-27 04:31:51

您会看到默认值开始生效。

摆脱控制器和操作的默认值。

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new {id = UrlParameter.Optional } // Parameter defaults
    );

这将使用户在控制器中键入:行动。

You are seeing the default values kick in.

Get rid of the default values for controller and action.

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new {id = UrlParameter.Optional } // Parameter defaults
    );

This will make a user type in the controller & action.

遥远的她 2024-10-27 04:31:51

我不认为你这样做是为了 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文