I'm trying to pull the name of the current View from within my Layout.
Usually, VirtualPath
is used for this. Unfortunately, this will return the path of the Layout file.
Is there any way to get the name of the View that's returned by the controller?
The following will get you the view name:
If you use MVC Core you can use:
You can use ViewBag. Define a
CurrentView
property to it and use that.And in the layout, you can read and use it like
Or if you want to get it into a variable
If you do not wish to explicitly set the viewbag property name, you can write a custom action filter to do that.
And you need to decorate your action methods with our new action filter