Reputation: 11403
O.K. I think that's an easy one!
I have a ViewMasterPage with some links on it (Home/About/Login/ etc). What I want to achieve is simply have the link disabled when it's referral url is already opened (i.e. if the current url is /Register then the Register link should be disabled .. easy hah!?)
As I don't like to write a lot of in-line coding inside of my views, I end up extending HtmlHelper with some extension methods (just to keep the code in .cs files) and in my views I call these methods, here's my register method as an example:
public static string Register (this HtmlHelper html)
{
TagBuilder builder ;
if (HttpContext.Current.Request.Url.AbsoluteUri.ToUpperInvariant().Contains(MainLinks.Register.ToUpperInvariant()))
return MainLinks.Register; // will return the string "Register"
builder = new TagBuilder("a");
builder.InnerHtml = MainLinks.Register;
builder.AddCssClass("register");
builder.Attributes.Add("href", "/register/");
return builder.ToString();
}
Though this works, it still has two problems:
The hard coded string values of the urls (specially for the home link as I compare the AbslouteUri with "http://www.mysite.com/")
My programming instinct doesn't like it, I feel it should be much simpler than that.
Any ideas!
Ps: No javascipt allowed! It's a javascript-free version of the application.
Upvotes: 0
Views: 419
Reputation: 24532
I don't see too much wrong with this, it's clear to see what it does and it works. However, it's probably better to make it a bit more reusable as I can imagine you repeat yourself a bit with the other links. Maybe something like:
public static string RenderLink(this HtmlHelper html, string text, string url, object htmlAttr) {
if (!HttpContext.Current.Request.Url.AbsolutePath.StartsWith(url, StringComparison.InvariantCultureIgnoreCase)) {
return text; //comparison excludes the domain
}
TagBuilder tag = new TagBuilder("a");
tag.SetInnerText(text);
tag.Attributes.Add("href", url);
//... add attributes parsed as htmlAttr here
return tag.ToString();
}
Then add your links to your view like:
<%= Html.RenderLink("Register", "/register/", new { @class="register"}) %>
<%= Html.RenderLink("Account", "/account/", new { @class="account"}) %>
If you wanted to get away from the hard coded domain, then using Request.Url.AbsolutePath instead of AbsoluteUri as above achieves that.
The alternative would be to parse the current page information in the model from the controller, maybe like ViewData.Model.CurrentPage = "Register";, but I wouldn't advise you doing that as I don't see it being the job of the controller in this case.
Upvotes: 1