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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With