How to get the current view name in asp.net MVC 3?
No idea why you would need to get the current view name but you could use the VirtualPath
property inside a view. Normally it's more useful to know the current action or controller. But anyway, here's how to get the current view name:
@VirtualPath
and if you wanted to get only the filename:
@Path.GetFileName(Server.MapPath(VirtualPath))
and without the extension:
@Path.GetFileNameWithoutExtension(Server.MapPath(VirtualPath))
I've also tested this code, and I could do something with it. But, I'm not sure if is this a good solution or not.
For example, I need to detect the Contacts view located in Home directory. So I wrote:
if (@Request.RawUrl == "/Home/Contacts")
{
// do something
}