asp.net-mvc 错误“'~/Views/Page/home.aspx' 中的视图必须从 ViewPage、ViewPage<TViewData>、ViewUserControl 或 ViewUserControl<TViewDat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2467808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:12:45  来源:igfitidea点击:

Error "The view at '~/Views/Page/home.aspx' must derive from ViewPage, ViewPage<TViewData>, ViewUserControl, or ViewUserControl<TViewData>"

asp.net-mvcasp.net-mvc-2views

提问by Glenn Slaven

I've just installed MVC2 and I've got a view that looks like this

我刚刚安装了 MVC2,我有一个看起来像这样的视图

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Home.Master" Inherits="System.Web.Mvc.ViewPage" %>    
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home
</asp:Content>    
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">    
    <h2>Home</h2>    
</asp:Content>

And the controller is just returning the view. But when I run the page I get this error:

而控制器只是返回视图。但是当我运行页面时,我收到此错误:

System.InvalidOperationException: The view at '~/Views/Page/home.aspx' must derive from ViewPage, ViewPage, ViewUserControl, or ViewUserControl.

System.InvalidOperationException:“~/Views/Page/home.aspx”中的视图必须从 ViewPage、ViewPage、ViewUserControl 或 ViewUserControl 派生。

回答by Levi

You might unintentionally have two versions of the MVC framework loaded into the same application. To confirm, download MVC 2 Futures from http://aspnet.codeplex.com/releases/view/41742. There is a file MvcDiagnostics.aspxin this ZIP file.

您可能会无意中将两个版本的 MVC 框架加载到同一个应用程序中。要确认,请从http://aspnet.codeplex.com/releases/view/41742下载 MVC 2 Futures 。此 ZIP 文件中有一个文件MvcDiagnostics.aspx

  1. Copy MvcDiagnostics.aspx to the root of your web project.
  2. Reproduce the error.
  3. After reproducing the error, go to /MvcDiagnostics.aspx and see if it complains (will be in bold red lettering) about multiple versions of the framework being loaded. The tool should suggest a resolution if this is the case.
  1. 将 MvcDiagnostics.aspx 复制到 Web 项目的根目录。
  2. 重现错误。
  3. 重现错误后,转到 /MvcDiagnostics.aspx 并查看它是否抱怨(以粗体红色字体显示)正在加载多个版本的框架。如果是这种情况,该工具应建议解决方案。

Don't forget to delete MvcDiagnostics.aspx from your project when you are done.

完成后不要忘记从项目中删除 MvcDiagnostics.aspx。

回答by David

I was getting this error upgrading my MVC 1.0 project to MVC 2.0. If you are doing this have a look at http://weblogs.asp.net/leftslipper/archive/2010/03/10/migrating-asp-net-mvc-1-0-applications-to-asp-net-mvc-2-rtm.aspx

我在将我的 MVC 1.0 项目升级到 MVC 2.0 时遇到了这个错误。如果您这样做,查看http://weblogs.asp.net/leftslipper/archive/2010/03/10/migrating-asp-net-mvc-1-0-applications-to-asp-net-mvc -2-rtm.aspx

Double check your web.config against a new MVC 2.0 project.

针对新的 MVC 2.0 项目仔细检查您的 web.config。

回答by Bohemian

Doing either a batch->build->clean at the solution level OR a clean at the project level usually takes care of this if you have already done the due diligence of removing/replacing the undesired version(s) of System.Web.Mvcfrom project references and *.config files...

batch->build->clea如果您已经完成了System.Web.Mvc从项目引用和 *.config 文件中删除/替换不需要的版本的尽职调查,那么在解决方案级别执行n 或在项目级别执行清理通常会解决这个问题...

回答by superlogical

You could try using the ASP.NET MVC 3 Application Upgrader

您可以尝试使用ASP.NET MVC 3 Application Upgrader

Best to do this with your code under source control, that way you can eye-ball everything that is changes. It updates your javascript which I didn't want so I reverted that, and only kept the changes it makes to the web.config for razor.

最好在源代码控制下使用您的代码执行此操作,这样您就可以观察所有更改。它更新了我不想要的 javascript,所以我恢复了它,并且只保留了它对 razor 的 web.config 所做的更改。

Be sure to read about what it does and what it doesn't support on this blog post.

请务必在此博客文章 中阅读它的功能和不支持的功能。

回答by BlackHymanetMack

I solved this problem for myself by having my custom view engine inherit RazorViewEngine directly.

我通过让我的自定义视图引擎直接继承 RazorViewEngine 为自己解决了这个问题。

public class MainViewEngine : RazorViewEngine
{
    public MainViewEngine()
    {...
}}

And then registering that viewengine in the Global.asax file Application_Start event:

然后在 Global.asax 文件 Application_Start 事件中注册该视图引擎:

ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new MainViewEngine());

Clearly this assumes that you are using Razor.

显然,这假设您正在使用 Razor。

回答by sree

Check the reference to System.Web.Mvc.dll file in your reference. Make sure its pointing to C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll

检查参考中对 System.Web.Mvc.dll 文件的引用。确保它指向 C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll

If its pointing to the one under ASP.NET MVC 4 directory then open your project file in a notepad. Look for

如果它指向 ASP.NET MVC 4 目录下的那个,则在记事本中打开您的项目文件。寻找

<Reference Include="System.Web.Mvc">
  <Private>False</Private>
</Reference>

This should be changed to

这应该改为

<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35, processorArchitecture=MSIL" />

This will help to point to the specific MVC version

这将有助于指向特定的 MVC 版本