Track the current active page, or how to get controller and action names in a view

Use

var controller = ViewContext.RouteData.Values["Controller"];
var action = ViewContext.RouteData.Values["Action"];

<script>
$("li#@(ViewContext.RouteData.Values["Controller"])-@(ViewContext.RouteData.Values["Action"])")).addClass("active");
</script>

PS: Use ToLower() if required

ViewContext.RouteData.Values["Action"].ToString().ToLower();

Also you can activate your menu by style block

<style>
li#@(ViewContext.RouteData.Values["Controller"])-@(ViewContext.RouteData.Values["Action"]) {
//add some style
}
</style>

With dot net core 2.0 for razor pages you could use like this

var rv = ViewContext.RouteData.Values;
string page = $"{rv["page"]}".ToLowerInvariant();

On the tags

<li class="@(page == "/index" ?  "active" : "")"><a asp-page="/Index" >Home</a></li>

I would like to add something small that has perhaps been overlooked... Just add some embedded code in the layouts page at the top, before the HTML begins...

@{
    var controller = ViewContext.RouteData.Values["Controller"];
    var action = ViewContext.RouteData.Values["Action"];
}

The code is pretty self-explanatory... Then call the variables' values from anywhere within your code just as they are, like the way I did as shown below:

<div class="navbar-wrapper">
<h3>@controller | @action</h3>
</div>

I found this method to be much simpler and hassle-free than aforementioned methods. Cheerio ;)