In ASP.NET MVC5, how can I hide the Action name when an a tag is generated in Razor using Url.Action?

倖福魔咒の 提交于 2019-12-24 15:19:07

问题


As the title says.

I have a route set up and working fine, which provides a default action when none is specified. I just want to hide the action from the URL because it's unnecessary clutter.

Setting the "ActionName" parameter as null, or "", will just result in the current page's action being substituted instead - which doesn't work.

I'm open to using @Html.ActionLink() if that will get me what I need.

My route definition is

routes.MapRoute(
    name: "MyBookRoute", 
    url: "Book/{id}", 
    defaults: new { controller = "Book", action = "Index" }
);

If all else fails, I suppose I can deal with writing out the hrefs manually, but this should not be a difficult thing for Razor to do.

Has anyone else come across this and knows what to do?


回答1:


Base on your route definition, then either

@Url.Action("Index", "Book", new { id = 1 })

or

@Html.ActionLink("Your link text", "Index", "Book", new { id = 1 }, null)

will remove the action name from the generated url.



来源:https://stackoverflow.com/questions/29729098/in-asp-net-mvc5-how-can-i-hide-the-action-name-when-an-a-tag-is-generated-in-ra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!