tags:

views:

42

answers:

1

Essentially, the question is in the title. I have a ASP.NET MVC application, and I need to get and use the name of the view in the master page.

For example, I may want to include a "viewname".js in the header of every page or use it in RenderAction call. Of course, I can create asp:ContentPlaceHolder in the head section of master page, but I noticed that the contents that I am putting in the views is all the same with the exception of the view name itself. So, I am trying to refactor, and I don't see anything helpful in either Page, or any ViewXXX variables.

Thanks

+2  A: 

You should be able to get the name of the view from:

IView view = ((ViewPage)this.Page).ViewContext.View;
string viewname = ((WebFormView)view).ViewPath;
Rex M
Thank you! I saw ViewPath in the debugger, but it didn't occur to me to do the cast! How stupid :/actually, after some more tinkering, everything can be combined in <%= ((WebFormView)ViewContext.View).ViewPath %>
Felix