Get VirtualPath Views Using ViewContext

Is it possible to calculate the current ViewContext VirtualPath, for example

~ / Views / Company / Create.aspx

+4
source share
2 answers

I think this is what you need:

((WebFormView) ViewContext.View).ViewPath 

Obviously, this only works if you use the standard mechanism for viewing web forms (which is based on the wording of your question, you). That is, ViewContext.View returns an ViewContext.View , which cannot be a WebFormView if you use a different viewing mechanism.

Change By the way, I tested the code above, and it definitely returns the path as a virtual path (and not the absolute or relative path of the application, as I assumed, suggested that it can). That is, it gives you a value similar to this:

 ~/Views/Company/Create.aspx 
+4
source

This code can be used to get the virtual path,

 <%= HttpRuntime.AppDomainAppVirtualPath %> 
0
source

All Articles