What's the base class of a Razor View in ASP.NET MVC3?

Diego System.Web.Mvc. WebViewPage is the right base type (and you should have another class inheriting from System.Web.Mvc. WebViewPage You should mark your own class as abstract so that you are not forced to implement the Execute method.

Up vote 12 down vote favorite 2 share g+ share fb share tw.

I'm trying to have all my views inherit from a custom class so that I can add certain behaviour and values to all pages, but I'm having some issues. I tried subclassing System.Web.Mvc. WebViewPage but I'm forced to implement an Execute procedure that I don't know what it should do.

Also, if I try to access the Context variable, I get a null reference (really weird). This leads me to think that I may have the wrong base class.... Any thoughts? Asp.net razor asp.

Net-mvc-3 link|improve this question asked Oct 18 '10 at 22:14Diego8181522 88% accept rate.

– R0MANARMY Oct 18 '10 at 22:18 Mainly I need to add some variables that can be referenced from within the views. I know I could achieve this using extension methods, but there should be a way of having custom viewpage classes as with the aspx view engine. – Diego Oct 18 '10 at 22:23.

Diego, System.Web.Mvc. WebViewPage is the right base type (and you should have another class inheriting from System.Web.Mvc. WebViewPage if you want strongly-typed views).

You should mark your own class as abstract so that you are not forced to implement the Execute method. Update: To configure all your views to use your custom base class, look into the ~\Views\Web. Config file.

Inside of it there's a Razor-specific section where you can use the pageBaseType attribute to configure your custom type. As far as the Context property is concerned, it should be fully initialized once the view is executing. However, it might not be available if you try to access it too early (for example, from your classes constructor).

When are you trying to access it? The Execute method is something that is provided by the Razor compiler when your view is compiled. For example, given the following view file The Razor compiler will behind the scenes generate the following class (this is a simplification, so the details might be off, but it should convey the point) public class _Some_Generated_Class_Name_ : System.Web.Mvc.

WebViewPage { public void Execute() { Write(" Write("! "); } } Then the framework calls the Execute method on your view class and your view gets executed.

In my Class's constructor :) That must be it then. – Diego Oct 19 '10 at 20:49 Just as an addendum to the question, what is the Execute procedure supposed to do? Is it okay if I just override it with a blank procedure?

– Diego Oct 19 '10 at 20:50 @Diego I've edited my answer to illustrate how the compiler generates an implementation for Execute. – marcind Oct 19 '10 at 22:57 Is there any way of specifying the base class for all views in a directory? I'd like to have normal views inherit from my particular System.Web.Mvc.

WebViewPage subclass and views that specify a model to inherit from my particular System.Web.Mvc. WebViewPage subclass. – Diego Oct 20 '10 at 18:34 @Diego i've added an update describing where to configure your base class.

– marcind Oct 20 '10 at 19:06.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions