The view must derive from WebViewPage

2019-06-21 13:10发布

问题:

I had a working MVC application when I decided that it was time for some maintenance. My views were all under SHARED directory and I decided to move them into separate directories.

I implemented a CustomViewEngine that I found here: Can I specify a custom location to "search for views" in ASP.NET MVC?

View structure looks something like this

Views
    AppViews
    OtherAppViews
    ...
    Shared
    ...

This is an error that I am getting:

The view at '~/Views/AppViews/SomeView.cshtml' must derive from ViewPage, ViewPage<TModel>, ViewUserControl, or ViewUserControl<TModel>.

I tried to inherit from @inherits System.Web.Mvc.WebViewPage (as explained here: The view must derive from WebViewPage, or WebViewPage<TModel>) , but when I do this I get an error saying that I can't use @inherits and @model at the same time.

回答1:

Your CustomEngine should derive from RazorViewEngine instead of WebFormViewEngine since you use .cshtml files (Razor engine).

Docs:

  • RazorViewEngine
  • WebFormViewEngine