Alex Rivera | Logout

Is there a way to include a fragment identifier when using Asp.Net MVC ActionLink, RedirectToAction, etc.?

Asked 2008-08-08T03:54:08.090
22

I want some links to include a fragment identifier. Like some of the URLs on this site:

Debugging: IE6 + SSL + AJAX + post form = 404 error#5626

Is there a way to do this with any of the built-in methods in MVC? Or would I have to roll my own HTML helpers?

Edit
Report

1 Answer

1

The short answer is: No. In ASP.NET MVC Preview 3 there's no first-class way for including an anchor in an action link. Unlike Rails' url_for :anchor, UrlHelper.GenerateUrl (and ActionLink, RedirectToAction and so on which use it) don't have a magic property name that lets you encode an anchor.

As you point out, you could roll your own that does. This is probably the cleanest solution.

Hackily, you could just include an anchor in a route and specify the value in your parameters hash:

routes.MapRoute("WithTarget", "{controller}/{action}/{id}#{target}");
...
<%= Html.ActionLink("Home", "Index", new { target = "foo" })%>

This will generate a URL like /Home/Index/#foo. Unfortunately this doesn't play well with URL parameters, which appear at the end of the URL. So this hack is only workable in really simple circumstances where all of your parameters appear as URL path components.

answered 2008-08-10T10:22:59.670

Your Answer