Freigeben über


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

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<TModel>.

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<TModel>. 

image

 

To easily understand this issue, we need to understand the WebViewPage class first. Here is the MSDN documentation https://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.

 <?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, 
                  System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral,PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, 
               Version=2.0.0.0, Culture=neutral,PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor,
               Version=2.0.0.0, Culture=neutral,PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>
  
  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage" ></pages>
  </system.web.webPages.razor>
</configuration>
 

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.

 

image 

Please let me know the feedback or concerns.

Thanks,

Gaurav

Comments

  • Anonymous
    January 14, 2014
    Nice one thanks a lot Gaurav

  • Anonymous
    June 01, 2014
    Helped me, thanks! Although i had to update the assembly versions to reflect the current versions i used in my project.

  • Anonymous
    October 28, 2014
    Is there a resolution to this yet?  Even with the section included in the web.config. the error is still thrown.

  • Anonymous
    November 18, 2014
    i have configured my View/Web..config with same configuration that you discussed in section 2 but i am still facing same issue... please guide me

  • Anonymous
    February 24, 2015
    Unfortunatelly, There is no solution for me :(

  • Anonymous
    May 10, 2015
    Hi, I am also facing the same issue but the mention solutions are not working for me. Please suggest me if i can try somethings additional to resolve this. I am using VS 2013, with MVC 4.5.

    • Anonymous
      March 29, 2017
      Hi,I was also facing with this problem and above solution did not helped me but i fixed it in other way.I opened webcofig file located in root folder and change key:from: to: No other actions were required. Maybe it will help You. Good luck.
  • Anonymous
    June 02, 2015
    Its wonderful articles, thanks issue my resovled. please send you contact mail id so i can send my mvc problems shrikantsmane@gmail.com

    • Anonymous
      January 14, 2017
      you think this guy is here to solve all your mvc problems? lol
  • Anonymous
    November 30, 2016
    Nice , it's resolved my issue , thanks Gaurav

  • Anonymous
    January 05, 2017
    Great article. Really helped me,. Thanks

  • Anonymous
    March 30, 2017
    Thanks it works

  • Anonymous
    June 19, 2017
    Nice articles ,thanks issue my resolved.

    • Anonymous
      December 12, 2017
      Thank you everyone. Good to know that article helped many.
  • Anonymous
    October 11, 2017
    Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.I am also facing the same issue but the mention solutions are not working for me. Please suggest me if i can try somethings additional to resolve this. i'm using vs15 mvc5