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

  • Home
  • SEARCH
  • 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 6786863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:17:45+00:00 2026-05-26T17:17:45+00:00

For example, here in StackOverflow you can se a top menu with the options:

  • 0

For example, here in StackOverflow you can se a top menu with the options: Questions, Tags, Users, Badges, Unanswered and Ask Question. When you are in one of those sections, it is highlighted in orange.

What is the best way to achieve that in ASP.NET MVC?

So far, and as proof of concept, I have done this helper:

    public static String IsCurrentUrl(this UrlHelper url, String generatedUrl, String output)
    {
        var requestedUrl = url.RequestContext.HttpContext.Request.Url;

        if (generatedUrl.EndsWith("/") && !requestedUrl.AbsolutePath.EndsWith("/"))
            generatedUrl=generatedUrl.Substring(0, generatedUrl.Length - 1);

        if (requestedUrl.AbsolutePath.EndsWith(generatedUrl))
            return output;

        return String.Empty;

    }

That method add the output string to the element if the current request match that link. So it can be used like this:

<li>
    <a href="@Url.Action("AboutUs","Home")" @Url.IsCurrentUrl(@Url.Action("AboutUs", "Home"), "class=on")><span class="bullet">About Us</span></a>
 </li>

First problem, I am basically calling twice to Url.Action, first for the “href” attribute, and after in the helper, and I think there has to be a better way to do this. Second problem, that is not the best way to compare two links. I think I could create a new Html.ActionLink overload so I don’t need to call the Url.Action twice, but is there any buil-in way to do this?

Bonus: if I add "class=\"on\"", MVC renders class=""on"". Why?

Regards.

  • 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-26T17:17:46+00:00Added an answer on May 26, 2026 at 5:17 pm

    For a project that i’m working on we’ve had the exact same problem. How to highlight the current tab? This is the approach that was taken at the time:

    In the master page view:

     <% 
       var requestActionName = 
                             ViewContext.RouteData.Values["action"].ToString();
       var requestControllerName = 
                             ViewContext.RouteData.Values["controller"].ToString();
     %>
    
     <li class="<%=  requestActionName.Equals("Index",
                       StringComparison.OrdinalIgnoreCase)
                       && requestControllerName.Equals("Home",
                       StringComparison.OrdinalIgnoreCase) ? 
                       "current" : string.Empty %>">
                <%: Html.ActionLink("Home", "Index", "Home") %>
      </li>
    

    Basically what’s happening is that we’re just string comparing the action and controller values with values associated with a link. If they match, then we’re calling that the current link, and we assign a ‘current’ class to the menu item.

    Now so far, this works, but as we’ve gotten bigger in size, this setup starts to get pretty large with a whole lot of ‘or’ this ‘or’ that. So keep that mind if you decide to try this.

    Good luck, and hope this helps you out some.

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

Sidebar

Related Questions

For example, here on stack overflow the URL https://stackoverflow.com/questions/tagged/javascript+php will give you all questions
I have a several questions about hibernate. In many questions here in stackoverflow, several
i've found a shake effect in stackoverflow question ( here ) Code is like
I saw in an earlier post here on stackoverflow a example on many to
I'm following a simple ajax>php>mysql example posted here http://openenergymonitor.org/emon/node/107 I can only display information
I had a look at other stackoverflow questions and couldn't find one that asked
I playing around with the sftp example from here: Stackoverflow: twisted conch filetransfer I
The fictional example here. Suppose I designed a class named IODevice. This class already
I found a great C++/ODBC example here ... The project I downloaded builds great
This problem is not readily reproducible in a simple example here but was wondering

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.