gpt4 book ai didi

c# - MVC 特殊路由

转载 作者:行者123 更新时间:2023-11-30 16:30:56 26 4
gpt4 key购买 nike

我有一个 asp.net MVC 项目,我需要在其中定义一些自定义路由。类似于您在 Wordpress 上看到的帖子,其中路由的形式为 postid-postname:

12-i-am-post

我知道如何做类似postid/postname的事情:

12/i-am-post.

但是我如何制作一个结合了两者的路由,比如:

mywebsite.com/12-postname-is-her

最佳答案

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


public ActionResult Index(int id, string postName)
{
return View();
}

应该适用于以下请求 http://localhost/Post/1-MyPostName

关于c# - MVC 特殊路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5255870/

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