In ASP.NET MVC5, how can I hide the Action name wh

2019-08-02 11:59发布

问题:

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.