开发者

mvc actionlink parameters change route url

so i have an actionlink line on my action

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

it produces this URL, (if item.Id is 1):

http://localhost:1111/company/profile/1
开发者_Python百科

is it possible to produce the url/route as (basically using item.Name instead of the Id:

http://localhost:1111/company/profile/nameofcompany1

wasn't really sure how to handle to handle the routing of this..if i should change the routing on the global.asax for the controller.

thoughts are welcome.

Thanks!

-G


Sure:

<% Html.ActionLink (item.Name, "Profile", new { name = item.Name } )%>

Now of course this assumes that you modify your route definitions and replace the id token by name and also replace the id action parameter in your controller actions by name.

This being said, the usefulness of such urls seems pretty limited to me. What if you have 2 items with the same name? I would recommend you always using ids in order to identify your items in an unique manner. And if you want to much to have some name in this url, take for example how urls are implemented on StackOverflow: they use question id, and append a slug (not the name, it's a filtered name because things can quickly get out of control if your names contain some special characters) at the end of the url.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜