I built html helper to change class
attribute based on current page. When using MVC, I can get the value for Controller and Action by using this method
string actualAction = (string) html.ViewContext.RouteData.Values["action"];
string actualController = (string) html.ViewContext.RouteData.Values["controller"];
Using razor pages
, I can get the razor page endpoint by using this code
var rv = html.ViewContext.RouteData.Values;
string page = $"{rv["page"]}".ToLowerInvariant();
So I will get the value such like /admin/dashboard
, /admin/configuration/add-user
, /admin/configuration/list-user
and etc.
At the endpoint, I want to know what is the first directory name. Following my example, the admin
is a root directory.
Is there is a .Net core function handles this? or need to customize?
You can use the HttpContext
. Check here on how to access it, depending where you are: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-3.1
Then access the path from the context request:
Context.Request.Path
To use it from the view
@{
var path = Context.Request.Path;
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.