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

<p><a href="https://stackoverflow.com/questions/5619/ie6-ssl-ajax-post-form-404-error">Debugging: IE6 + SSL + AJAX + post form = 404 error</a><strong>#5626</strong></p>

<p>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?</p>

[{'answer_id': 7151, 'author': 'Dominic Cooney', 'author_id': 878, 'author_profile': 'https://Stackoverflow.com/users/878', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>As you point out, you could roll your own that does. This is probably the cleanest solution.</p>\n\n<p>Hackily, you could just include an anchor in a route and specify the value in your parameters hash:</p>\n\n<pre><code>routes.MapRoute("WithTarget", "{controller}/{action}/{id}#{target}");\n...\n&lt;%= Html.ActionLink("Home", "Index", new { target = "foo" })%&gt;\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 30573, 'author': 'Haacked', 'author_id': 598, 'author_profile': 'https://Stackoverflow.com/users/598', 'pm_score': 4, 'selected': True, 'text': "<p>We're looking at including support for this in our next release.</p>\n"}, {'answer_id': 30614, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 2, 'selected': False, 'text': '<p>@Dominic,</p>\n\n<p>I\'m almost positive that putting that in the route will cause routing issues.</p>\n\n<p>@Ricky,</p>\n\n<p>Until MVC has support for this, you can be a little more "old school" about how you make your routes. For example, you can convert:</p>\n\n<pre><code>&lt;%= Html.ActionLink("Home", "Index") %&gt;\n</code></pre>\n\n<p>into:</p>\n\n<pre><code>&lt;a href=\'&lt;%= Url.Action("Index") %&gt;#2345\'&gt;Home&lt;/a&gt;\n</code></pre>\n\n<p>Or you can write your own helper that does essentially the same thing.</p>\n'}, {'answer_id': 1241929, 'author': 'LorenzCK', 'author_id': 3118, 'author_profile': 'https://Stackoverflow.com/users/3118', 'pm_score': 4, 'selected': False, 'text': '<p>As Brad Wilson wrote, you can build your own link in your views by simply concatenating strings. But to append a fragment name to a redirect generated via RedirectToAction (or similar) you\'ll need something like this:</p>\n\n<pre><code>public class RedirectToRouteResultEx : RedirectToRouteResult {\n\n    public RedirectToRouteResultEx(RouteValueDictionary values)\n        : base(values) {\n    }\n\n    public RedirectToRouteResultEx(string routeName, RouteValueDictionary values)\n        : base(routeName, values) {\n    }\n\n    public override void ExecuteResult(ControllerContext context) {\n        var destination = new StringBuilder();\n\n        var helper = new UrlHelper(context.RequestContext);\n        destination.Append(helper.RouteUrl(RouteName, RouteValues));\n\n        //Add href fragment if set\n        if (!string.IsNullOrEmpty(Fragment)) {\n            destination.AppendFormat("#{0}", Fragment);\n        }\n\n        context.HttpContext.Response.Redirect(destination.ToString(), false);\n    }\n\n    public string Fragment { get; set; }\n}\n\npublic static class RedirectToRouteResultExtensions {\n    public static RedirectToRouteResultEx AddFragment(this RedirectToRouteResult result, string fragment) {\n        return new RedirectToRouteResultEx(result.RouteName, result.RouteValues) {\n            Fragment = fragment\n        };\n    }\n}\n</code></pre>\n\n<p>And then, in your controller, you\'d call:</p>\n\n<pre><code>return RedirectToAction("MyAction", "MyController")\n       .AddFragment("fragment-name");\n</code></pre>\n\n<p>That should generate the URL correctly.</p>\n'}, {'answer_id': 6768970, 'author': 'voam', 'author_id': 177395, 'author_profile': 'https://Stackoverflow.com/users/177395', 'pm_score': 0, 'selected': False, 'text': '<p>This is a client side solution but if you have jquery available you can do something like this.</p>\n\n<pre><code>&lt;script language="javascript" type="text/javascript"&gt;\n    $(function () {\n        $(\'div.imageHolder &gt; a\').each(function () {\n            $(this).attr(\'href\', $(this).attr(\'href\') + \'#tab-works\');\n        });\n    });\n&lt;/script&gt;\n</code></pre>\n'}, {'answer_id': 7193912, 'author': 'Joel', 'author_id': 236167, 'author_profile': 'https://Stackoverflow.com/users/236167', 'pm_score': 2, 'selected': False, 'text': "<p>In MVC3 (and possibly earlier I haven't checked), you can use UrlHelper.GenerateUrl passing in the fragment parameter. Here's a helper method I use to wrap the functionalityL</p>\n\n<pre><code>public static string Action(this UrlHelper url, string actionName, string controllerName, string fragment, object routeValues)\n{\n    return UrlHelper.GenerateUrl(\n        routeName: null,\n        actionName: actionName,\n        controllerName: controllerName,\n        routeValues: new System.Web.Routing.RouteValueDictionary(routeValues),\n        fragment: fragment,\n        protocol: null,\n        hostName: null,\n        routeCollection: url.RouteCollection,\n        requestContext: url.RequestContext,\n        includeImplicitMvcValues: true /*helps fill in the nulls above*/\n    );\n}\n</code></pre>\n"}, {'answer_id': 30172386, 'author': 'Theophilus', 'author_id': 2615878, 'author_profile': 'https://Stackoverflow.com/users/2615878', 'pm_score': 0, 'selected': False, 'text': '<p>Fragment identifiers are supported in MVC 5. See <code>ActionLink</code>\'s overloads at <a href="https://msdn.microsoft.com/en-us/library/dd460522(v=vs.118).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/dd460522(v=vs.118).aspx</a> and <a href="https://msdn.microsoft.com/en-us/library/dd492938(v=vs.118).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/dd492938(v=vs.118).aspx</a>.</p>\n'}]
['https://Stackoverflow.com/questions/5628', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/653/']