目前在我的项目中,我使用@ Url.Action在锚标签的href属性中创建链接.
例如:
<a href='@Url.Action("Index","Home",new { id = 10 })' id="btn">Click</a>
这工作正常,但产生的URL是……
/home/index?id=10
有没有办法返回网址
/home/index/10
对于SEO和审美目的?我使用id和number作为占位符 – 在实际应用程序中它可以并将使用字符串代替(所以…
<a href='@Url.Action("Index",new { name="test" })' id="btn">Click</a>
返回网址
/home/index/test
解决方法
您需要确保路由正确.确保你有这样的路线:
routes.MapRoute( name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home",action = "Index",id = UrlParameter.Optional });
对于“真正的应用程序”,当您使用名为name的参数时,您将需要如下所示:
routes.MapRoute( name: "Default",url: "{controller}/{action}/{name}",action = "Index" });