Get Current Area Name in View or Controller
From MVC2 onwards you can use ViewContext.RouteData.DataTokens["area"]
How do you get the current MVC Area name in a .cshtml file in ASP.NET Core
Thanks to Pranav's comment! GetNormalizedRouteValue
method is now public. So you can call RazorViewEngine.GetNormalizedRouteValue
method in your view to the the area name when you pass the key as "area".
<h2> @RazorViewEngine.GetNormalizedRouteValue(ViewContext, "area") </h2>
RazorViewEngine
class belongs to Microsoft.AspNetCore.Mvc.Razor
namespace. So make sure you have a using statement to import that in your view.
@using Microsoft.AspNetCore.Mvc.Razor
GetNormalizedRouteValue
method is doing the same as explained in the previous version of answer. You can see the source code here
This totally worked for me in a razor view (MVC6).
@using Microsoft.AspNet.Routing
@{
var myAreaName = string.Empty;
object areaObj;
if (ViewContext.RouteData.Values.TryGetValue("area", out areaObj))
{
myAreaName = areaObj.ToString();
}
}
<h1>@myAreaName</h1>
Get Area name in Views on render time at ASP.NET MVC3 Razor
The Area name is not in the RouteData.Values
but you can get it from the DataTokens collection::
ViewContext.RouteData.DataTokens["area"]
How to get the current Action name inside the View
Basically, in my _Layout page, I wanted to show or hide Search form if the user is in the index view.
Try with below codes :
@if ("Index".Equals(ViewContext.RouteData.Values["Action"].ToString()))
{
<form asp-action="Index" method="get" class="navbar-form form-inline navbar-right">
<input class="form-control mr-sm-2" id="search" name="search" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" id="BtnSearch" name="BtnSarch" type="submit">Search</button>
</form>
}
How to determine Controller's Area in ASP.NET MVC
ASP.NET MVC doesn't keep the AreaRegistration objects around, but AreaRegistration is how ASP.NET determines which Area a Controller belongs to.
You can just enumerate AreaRegistration types on your own within the relevant assemblies, then determine if the AreaRegistration's namespace is a prefix of the Controller's namespace. Once resolved, an AreaRegistration instance has the Area name as a property.
Rather than using reflection, you could include this in your AreaRegistration implementations themselves by providing a base class that implements RegisterArea and maintains a Namespace=>Area map.
Get area name from ActionExecutingContext
From MVC Sourcecode:
static string GetAreaName(RouteBase route)
{
var area = route as IRouteWithArea;
if (area != null)
{
return area.Area;
}
var route2 = route as Route;
if ((route2 != null) && (route2.DataTokens != null))
{
return (route2.DataTokens["area"] as string);
}
return null;
}
static string GetAreaName(RouteData routeData)
{
object obj2;
if (routeData.DataTokens.TryGetValue("area", out obj2))
{
return (obj2 as string);
}
return GetAreaName(routeData.Route);
}
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>
Get current controller in view
I have put this in my partial view:
@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()
in the same kind of situation you describe, and it shows the controller described in the URL (Category for you, Product for me), instead of the actual location of the partial view.
So use this alert instead:
alert('@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()');
ASP.NET MVC 5 - Get current view's name (Razor .cshtml side)
Since you are using the convention where your page is named after the controller you can do this in razor to get the controller/page name:
@{
var pageName = ViewContext.RouteData.Values["controller"].ToString();
}
<ul class="sidebar bg-grayDark">
<li class="@(pageName == "Home" ? "active" : "")">
<a href="@Url.Action("Index", "Home")">
<span class="mif-home icon"></span>
<span class="title">Home</span>
</a>
</li>
<li class="bg-hover-black @(pageName == "Product" ? "active" : "")">
<a href="@Url.Action("Index", "Product")">
<span class="mif-shop icon"></span>
<span class="title">Products</span>
<span class="counter">14</span>
</a>
</li>
<li class="bg-hover-black @(pageName == "Category" ? "active" : "")">
<a href="@Url.Action("Index", "Category")">
<span class="mif-flow-cascade icon"></span>
<span class="title">Categories</span>
<span class="counter">9</span>
</a>
</li>
<li class="bg-hover-black @(pageName == "User" ? "active" : "")">
<a href="@Url.Action("Index", "User")">
<span class="mif-users icon"></span>
<span class="title">Users</span>
<span class="counter">1</span>
</a>
</li>
</ul>
This will set your active class on the page server side removing the need to do this client side with javascript.
Related Topics
Can You Explain Liskov Substitution Principle with a Good C# Example
How to Store Data Locally in .Net (C#)
Spawn Multiple Threads for Work Then Wait Until All Finished
How to Convert Securestring to System.String
Handling the Window Closing Event with Wpf/Mvvm Light Toolkit
Routing with Multiple Get Methods in ASP.NET Web API
Declaring a New Instance of a Class in C#
Internal VS. Private Access Modifiers
How to Make My Own Event in C#
How to Store/Retrieve Rsa Public/Private Key
How Does Task<Int> Become an Int
Why Is Try {...} Finally {...} Good; Try {...} Catch{} Bad