Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlHelper.Action: would like to generate a link that ends with '#something'

I'm trying to create a link with Url.Action, which ends with a #something; I presume there's something in the route values to do this properly, but I couldn't find it with Google.

So far, I tried Url.Action("action", "controller", new {id="something", Area="area"}). The resulting link is the expected /action/controller/area, but I can't tack the #something in the end.

Url-wise, I could probably get away with saying <a href="<%= Url.Action(..) %>#something"> but that doesn't strike me as particularly nice; I'm looking for a better solution.

like image 894
Liz Avatar asked Sep 13 '10 08:09

Liz


People also ask

What is action method in url?

Action method only creates the url not the complete hyperlink, to create hyperlink we need to use Html. ActionLink covered next. To access these querystring values in the action method, we can use Request.QueryString like below. CONTROLLER ACTION METHOD public ActionResult Index() { string com = Request.

What is difference between HTML ActionLink and url action?

There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


1 Answers

There is no overload of the Url.Action() method that does this for you. Ether you will have to do it in the way you suggest (by simply adding it after the call to Url.Action()) or create your own extension method.

Your extension method can look something like this:

public static MvcHtmlString Action(this UrlHelper urlHelper, string action, string controller, string hash)
{
    return string.Format("{0}#{1}", urlHelper.Action(action, controller), hash);
}
like image 92
Mattias Jakobsson Avatar answered Oct 01 '22 02:10

Mattias Jakobsson