I am writing this blog to help you understand the following scenario which you can see sometime while working on the ASP.NET MVC website. You are prone to get this error message if you are using ASP.NET Razor view syntax and specifically

not using the Visual Studio provided template to develop an ASP.NET MVC website or you have removed some files while customizing the template. Let’s try to understand this scenario.

 

The view at '~/Views/home/index.cshtml' must derive from WebViewPage, or WebViewPage.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.InvalidOperationException: The view at '~/Views/home/index.cshtml' must derive from WebViewPage, or WebViewPage.  

 

To easily understand this issue, we need to understand the WebViewPage class first. Here is the MSDN documentation http://msdn.microsoft.com/en-us/library/system.web.mvc.webviewpage(v=vs.118).aspx which says “WebViewPage class represents the properties and methods that are needed in order to render a view that uses ASP.NET Razor syntax.”

 

So since we know that this is required component to get Razor syntax working, we need to find out the solution now. Easiest solution can be #1 given following and which is as per the error message says, view must derive from WebViewPage.

 

Solution 1.

Add following line on top of your cshtml file.

@inherits System.Web.Mvc.WebViewPage

 

You must be wondering now thinking that views in ASP.NET MVC templates do not have this line on top of the cshtml file? So let’s see the second solution.

Solution 2.

Add a web.config file  and specify the same setting for all the views. This  is the minimum required code in this configuration file to get rid of this error message.



  
    
      
 
        

Actual setting required is pageBaseType="System.Web.Mvc.WebViewPage". Other text is only required to define the tags.

 

If you will create an ASP.NET MVC website using the Visual Studio provided default templates, you can see that Views folder already contains this web.config file. However, this file contains a lot of other required configuration as well.