Named Anchors And MVC Urls

4.08 (12 votes)

Named Anchors are used to generate links to specific locations within a web document. Unless you are careful, you may end up having a little trouble getting these to work using the MVC LinkExtension helpers.

Named anchors require the use of the name attribute within an <a href> element. If you look at the source code of any page on my site that contains an article with comments, you can see that the Comments section is preceded by one of these:


<a href="#" name="comments"></a>
<h2>Comments</h2>

You can also see (where there are a number of comments) that each one is "bookmarked" with its own uniquely named anchor:


<a href="#" name="commentId1033"></a>
<div class="comment">
...      
<a href="#" name="commentId1034"></a>
<div class="comment">
...
<a href="#" name="commentId1035"></a>
<div class="comment">
...

To get these to work, so that the browser takes the user to the position in the document defined by the anchor, the URL needs to be appended with #<anchor-name>, where <anchor-name> corresponds to the name attribute in the link. Valid examples would be:

http://www.mikesdotnetting.com/Article/129/Simple-task-Scheduling-using-Global.asax#commentId1031
http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet#comments

If you try to generate these types of URLs dynamically by concatenating IDs of items with strings, you may end up finding that the resulting URL contains %23 where you expected the hash or pound (#) sign. It gets UrlEncoded. And the link doesn't work as expected.

Both the ActionLink and RouteLink extension helpers contain an overload which overcomes this problem, but just looking at the current list of overload options doesn't necessarily make this obvious. Once you understand that the official HTML spec refers to the part of the URL after the # as a fragment identifier, it should become more obvious that the overload needed is this one (for the RouteLink):

LinkExtensions RouteLink method (HtmlHelper, String, String, String, String, String, Object, Object)

The fifth String argument takes the "fragment", or part after the # sign.

The Most Recent Comments panel (at the top right of my home page) uses this overload to generate the links behind the comment provider's name:


<%= Html.RouteLink(
  item.CommentName,                    // Link Text
  "Show",                              // Route Name
  null,                                // Protocol
  null,                                // Host Name
  "commentId" + item.CommentID,        // Fragment
  new {                                // Route Values
        controller = "Article", 
        action = "Show",
        id = item.Articles.ArticleID,
        title = item.Articles.Headline.ToCleanUrl()
      }, 
  null                                 // HTML Attributes
    ) %>

 

You might also like...

Date Posted:
Last Updated:
Posted by:
Total Views to date: 23128

0 Comments

Recent Comments

ojorma 17/06/2017 09:24
In response to Razor Pages - The Elevator Pitch
Finally I can say goodbye to webforms...

Mau 06/06/2017 08:58
In response to Razor Pages - The Elevator Pitch
I am missing the razor view code. Thank you for your article. Design pattern should improve the of...

Obinna Okafor 26/05/2017 16:16
In response to Razor Pages - The Elevator Pitch
Thank you for this wonderful piece, Mike. I need more of these...Keep them coming - anything more...

Cyrus 26/05/2017 06:00
In response to Razor Pages - The Elevator Pitch
There are some concern about razor pages performance. Is it faster or slower than MVC? would you a a...

Cyrus 26/05/2017 05:44
In response to Razor Pages - Understanding Handler Methods
well done, thank you....

Satyabrata Mohapatra 23/05/2017 11:41
In response to Razor Pages - Understanding Handler Methods
Nice and easy !! Great post....

Cyrus 16/05/2017 19:55
In response to Razor Pages - Getting Started With The Preview
There is something wrong related to microsoft.dotnetcore.mvc.taghelpers! if you remove it from page...

Cyrus 16/05/2017 10:18
In response to Razor Pages - Getting Started With The Preview
well done mike, it was very useful, I really appreciate that....

Satyabrata Mohapatra 16/05/2017 07:21
In response to Razor Pages - Getting Started With The Preview
Finally!!!! web pages in asp.net core!!! Super excited !!!! Thank u sir for sharing.....Awaiting on...

Daniele 14/03/2017 10:24
In response to Working With Zip Files In ASP.NET MVC
is it possible give to the user a progress bar of the zipping process? Thanks in advance. ...