5

I am getting "must derive from WebViewPage" error when calling PartialView inside other folder instead Views folder.

Error

System.InvalidOperationException: The view at '~/Modules/HtmlContent/_HtmlContent.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.
PartialView adding

Page.cshtml

<div class="side">
    @Html.Action("Side")
</div>

HomeController.cs

public ActionResult Side()
{
    return PartialView("~/Modules/HtmlContent/_HtmlContent.cshtml");
}

File Hierarchy

Modules
|
+-- HtmlContent
|             |
|             +-- _HtmlContent.cshtml
|
Views
    |
    +-- Home
    |      |
    |      +-- Index.cshtml
    |      |
    |      +-- Page.cshtml
    |
    +-- Shared
             |
             +-- Layout.cshtml
             |
             +-- _Partial.cshtml
user2282567
  • 781
  • 1
  • 7
  • 14

1 Answers1

14

You need to put a web.config in the root of this custom folder. The same as ~/Views/web.config. Inside this web.config you will find a section which indicates the type of the Razor pages:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
</system.web.webPages.razor>

Notice how the pageBaseType is set to System.Web.Mvc.WebViewPage.

Alternatively you could add the following to the top of your Razor views:

@inherits System.Web.Mvc.WebViewPage
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Hi @Darin Dimitrov, i tried you proposed solution. Actually I have created Area. I have adding a web.config file in my Area View Folder but still no luck. Alternate solution is working but you know it is hard to add for each view. – mmushtaq Mar 12 '17 at 17:20