Get the current controller and actions on the main page

using asp.net mvc. I have various links on the main page. eg,

/Home/

/ Home / About

/News/

/Documents/

if I select a link, I want the class to be installed on it. for example, if I select the "News" link, I want to highlight it by setting a class for it.

How can I determine which controller and action they need to set for its class attribute?

+3
source share
1 answer

Routingata will contain this information. The key "controller" will contain the name of the controller, and the key "action" will contain the name of the action.

, , body, . - :

<body id="<%=Html.GetBodyId()%>">

GetBodyId() :

public static string GetBodyId(this HtmlHelper helper) {
    return string.Format("{0}-{1}", 
        helper.ViewContext.RouteData.GetRequiredString("controller"), 
        helper.ViewContext.RouteData.GetRequiredString("action");
}

, :

<a href="[[link]]" class="home-index-link">Home</a>

css , -. :

.home-index-link {
    /*css rules here*/
}
#home-index .home-index-link {
    /*css for selected link*/
}
+3

Source: https://habr.com/ru/post/1732518/


All Articles