ASP.NET MVC - An HTML Extension Method That Creates a URL or Link

Consider an extension method whose purpose is either:

  • display tag <a>
  • under some condition, just return the string without a link

Question: in the extension method, how can you use the correct routing logic using route values, etc., rather than hard-coded a string. I suspect that the HtmlHelper.GenerateRouteLink is part of the solution, but please suggest a better way to achieve this.

 public static string CreateUserLink(this HtmlHelper html, string userAcctName) { if (string.IsNullOrEmpty(userAcctName)) return "--Blank--"; //some lookup to AD DomainUser user = ADLookup.GetUserByAcctName(userAcctName); if (user == null) return userAcctName; //would like to do this correctly! return string.Format("<a href='/MyAppName/User/View/{0}' title='{2}'>{1}</a>" , user.Mnemonic, user.DisplayName, user.Location); //normally returns http://mysite.net/MyAppName/User/View/FOO } 

Additional Information:

  • using ASP.NET MVC 1.0

alt text

+4
source share
3 answers

I just needed to do something like this yesterday. There may be an easier way to do this, but it helps me to see exactly what is happening, so I do not assume anything.

 public static string CreateUserLink(this HtmlHelper html, string userAcctName) { if (string.IsNullOrEmpty(userAcctName)) return "--Blank--"; //some lookup to AD DomainUser user = ADLookup.GetUserByAcctName(userAcctName); if (user == null) return userAcctName; RouteValueDictionary routeValues = new RouteValueDictionary(); routeValues.Add("controller", "User"); routeValues.Add("action", "View"); routeValues.Add("id", user.Mnemonic); UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext); TagBuilder linkTag = new TagBuilder("a"); linkTag.MergeAttribute("href", urlHelper.RouteUrl(routeValues)); linkTag.MergeAttribute("title", user.Location); linkTag.InnerHtml = user.DisplayName; return linkTag.ToString(TagRenderMode.Normal); } 
+7
source

will it work?

 public static string CreateUserLink(this HtmlHelper html, string userAcctName) { if (string.IsNullOrEmpty(userAcctName)) return "--Blank--"; //some lookup to AD DomainUser user = ADLookup.GetUserByAcctName(userAcctName); if (user == null) return userAcctName; return html.ActionLink(user.DisplayName, "user", "View", new {title=user.Location}); //normally returns http://mysite.net/MyAppName/User/View/FOO } 
+2
source

My experience with GenerateRouteLink was a tough battle. It has been a while since I messed it up, but if this is the method that I think of Microsoft made it β€œinternal”, so you cannot access and use it outside of the MVC assembly. There are a number of workarounds that I played with, and didn't really like.

What I ended up doing to avoid hard url encoding in my helper methods is to accept the 'string url' parameter and use Url.Action in my view when I call the helper method. This is not the cleanest, but it is a workaround that worked well for me.

 <%= Html.CreateUserLink("userAcctName", Url.Action("Home", "Controller") %> 
+1
source

All Articles