MVC ActionLink的路线使用的URL的名称,而不是ID [英] mvc route actionlink url use name instead of id

查看:179
本文介绍了MVC ActionLink的路线使用的URL的名称,而不是ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我已经问这一点,但该解决方案并没有实际的意义。无论如何,我有我的看法是这样ActionLinks:

I think I already asked this but the solution didn't really made sense. Anyway, I have ActionLinks on my views like this:

foreach( var item in Model){
<%: Html.ActionLink(item.Name, "Details", new {id = item.Id}) %>
}

现在,当它到我的行动显然细节,这将是该网址

now, when it goes to my Action Details obviously this will be the url

/MyThings/Details/{id}

我在想,如果有可能显示命名该项目,而不是ID使用塞?或修改全球ASAX像这样:

I was wondering if it's possible to show the name of that item instead of the id Using a slug? or modifying the global asax like so:

/MyThings/Details/CarKeys

问题是名字必须是唯一的,是有办法,我可以修改全局ASAX显示名称,而不是ID?

The thing is the names need to be unique, is there a way I can modify the global asax to show the name instead of the id?

任何意见得多AP preciated!

Any advice is much appreciated!

推荐答案

当然,你只需要改变你的路由规则。看在你的Global.asax.cs文件,或者在您的区域注册文件,这样的事情:

Sure, you just have to change your routing rules. Look in your Global.asax.cs file, or in your area registration file, for something like this:

routes.MapRoute(..., "{controller}/{action}/{id}", ...);

...并改变它的东西是这样的:

... and change it to something like this:

routes.MapRoute(..., "{controller}/{action}/{name}", ...);

然后让你采取行动,而不是ID名称:

Then have your action take the name instead of the ID:

Html.ActionLink(item.Name, "Details", new {item.Name})

这篇关于MVC ActionLink的路线使用的URL的名称,而不是ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆