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

2

5

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.

Oatmeal answered 13/9, 2010 at 8:41 Comment(0)
P
13

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);
}
Parmentier answered 13/9, 2010 at 8:46 Comment(1)
It's good to keep in mind, no doubt :) It's a little too complicated for what I had to do, eventually, I went with the 'url-wise' solution.Oatmeal
W
0

You should use one of the LinkExtensions.ActionLink methods. Documentation can be found here: http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx

Wozniak answered 15/7, 2011 at 21:48 Comment(1)
This works, if you wanted to use Html.ActionLink instead of Url.Action. Use this overload and put your hash in the fragment parameter. ActionLink(HtmlHelper, String, String, String, String, String, String, Object, Object)Witting

© 2022 - 2024 — McMap. All rights reserved.