gpt4 book ai didi

asp.net-mvc - MVC 路由 - 避免同一位置的多个 URL

转载 作者:行者123 更新时间:2023-12-01 11:57:53 24 4
gpt4 key购买 nike

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?

最佳答案

如果你只想要:

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" }
);

关于asp.net-mvc - MVC 路由 - 避免同一位置的多个 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5147317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com