MVC 3.net I want to add an anchor to the end a url.
I tried to include an anchor query string but the hash '#' changes to %23 or something like that in the url.
Is there a way of working around this?
The hypertext reference, or href , attribute is used to specify a target or destination for the anchor element. It is most commonly used to define a URL where the anchor element should link to. In this example, the <a href="http://example.com">anchored text</a> links to the URL <em>www.example.com</em>.
There is an overload of the ActionLink helper that allows you to specify the fragment:
@Html.ActionLink(
"Link Text", // linkText
"Action", // actionName
"Controller", // controllerName
null, // protocol
null, // hostName
"fragment", // fragment
new { id = "123" }, // routeValues
null // htmlAttributes
)
will produce (assuming default routes):
<a href="/Controller/Action/123#fragment">Link Text</a>
UPDATE:
and if you wanted to do this within a controller action performing a redirect you could use the GenerateUrl method:
public ActionResult Index()
{
var url = UrlHelper.GenerateUrl(
null,
"Action",
"Controller",
null,
null,
"fragment",
new RouteValueDictionary(new { id = "123" }),
Url.RouteCollection,
Url.RequestContext,
false
);
return Redirect(url);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With