ASP.NET MVC by default uses WebForms as the default View Engine. One gotcha you may not notice when adding a new *.aspx or *.master file, is that the respective codebehind classes (autogenerated ones) extend ViewPage and ViewMasterPage.
I really do not like associating a codebehind class to my .aspx or master files even when using webforms. And no, I do not write spaghetti code. Normally what I have is that everything in my webform projects is a custom WebControl. Anyhow, back to MVC, so, with MVC i didn't think i'd want to start using codebehind because there was clearly no need for it. So i throw out my codebehind, remove the codebehind specific attributes in the page directive and attempt to run my page.
Following is a sample of what my index.aspx looks like after i stripped out the codebehind class :
<%@ Page Language="C#" %>
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
This however didn't go too well. I kept hitting errors about Html not existing, note that i have a line of code above : Html.Encode, which is really a call to the HtmlHelper class. Nor did it like ViewData, and errored on that too. As it turns out, and which was expected is that the ASP.NET webforms page and masterpage by default extend Page and MasterPage classes respectively.
This means that we have to manually inherit both ViewPage and ViewMasterPage in their directives since these classes extends our normal Page/MasterPage classes with a few more properties exposed, Html and ViewData being some of these.
the fix was to simply inherit PageView as below :
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage"%>
and voila, i'm made. The same goes for master pages. By default your *.master inherits MasterPage class. What you want to do is instead inherit the MVC specific masterpage, which is ViewMasterPage class as follow :
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
I know, this is all non-trival however I didn't see this documented anywhere, so in case you end up with a typical error message eg :
" CS0103: The name 'Html' does not exist in the current context " and you know you've referenced System.Web.Mvc etc, this might actually help you move forward :p
Update : And while were at it, how do you use generics in the inheirts attribute in the page directive ? Well, it turns out that you need to use CLR Compatible Type name.
The easiest and most effective method to getting this is to first get the output using the typeof() and retrieving the FullName of the type, which apparently is the CLR Compatible Type name and then add that in the inherits attribute eg :
<%= typeof(MyNameSpace.MyType<System.Web.Mvc.ViewPage>).FullName %>
This should output something like this :
MyNameSpace.MyType`1[[System.Web.Mvc.ViewPage, System.Web.Mvc, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35]]
The above is what you will add in your inherits attribute. If you care for a proper explanation, you are welcome to read the following two online resources :
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=104071
and
http://forums.asp.net/t/1193721.aspx