Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 351481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:38:52+00:00 2026-05-12T11:38:52+00:00

O.K. I think that’s an easy one! I have a ViewMasterPage with some links

  • 0

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:

  1. The hard coded string values of the urls (specially for the home link as I compare the AbslouteUri with “http://www.mysite.com/“)

  2. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:38:52+00:00Added an answer on May 12, 2026 at 11:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Think that I have many activities,and all I want is this: I have a
just think that when I opened my file then when I want to write
I think that Visual Studio's biggest let down is the Javascript editor. I have
I think that some newer languages like JS can do this natively, but I
I think that is not easy to understand what I need reading title, so
I think that I have a quite strange question. I am using a class
I think that this is a relatively simple question. I just want to know
I think that such a list would be useful, but I have been unable
I think that it's really crazy question but anyway I want to know if
I think that there are some issues with the earth Gravity, so I wonder

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.