asp.net-mvc ASP.Net MVC4 Root cshtml 和“不继承自 'System.Web.WebPages.WebPage”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14402315/
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 02:33:22  来源:igfitidea点击:

ASP.Net MVC4 Root cshtml and "does not inherit from 'System.Web.WebPages.WebPage"

asp.net-mvcrazor

提问by DapperDanh

I have followed the answers provided by very similar posts as you will see in the step by step listed below.

我遵循了非常相似的帖子提供的答案,您将在下面列出的分步中看到。

I still have the same error message "does not inherit from 'System.Web.WebPages.WebPage'"

我仍然有相同的错误消息“不继承自 'System.Web.WebPages.WebPage'”

Overview

概述

I am learning from John Papa's "Single Page Apps with HTML5, Web API, Knockout and jQuery" on Pluralsight. The course outlines building an application called "Code Camper". The example MVC4 SPA creates a root view called "index.cshtml". where a series of @RenderPage calls are made. This application runs fine on my development machine. However, if i try to create from scratch a MVC4 SPA with a root view.cshtml I always get the error "does not inherit from 'System.Web.WebPages.WebPage"

我正在 Pluralsight 上学习 John Papa 的“带有 HTML5、Web API、Knockout 和 jQuery 的单页应用程序”。本课程概述了构建一个名为“Code Camper”的应用程序。示例 MVC4 SPA 创建了一个名为“index.cshtml”的根视图。其中进行了一系列 @RenderPage 调用。这个应用程序在我的开发机器上运行良好。但是,如果我尝试从头开始创建一个带有根 view.cshtml 的 MVC4 SPA,我总是收到错误“不继承自 'System.Web.WebPages.WebPage”

Step by Step

一步步

Download here.

在这里下载。

1.Create a new MVC4 Internet Project called "MVC4RootView"

1.新建一个名为“MVC4RootView”的MVC4互联网项目

2.In the root of the project, create a RootView.cshtml view.

2.在项目根目录下,创建一个RootView.cshtml视图。

@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>
    <div>
            @RenderPage("Views/Partial1.cshtml")
    </div>
</body>
</html>

3.Added a “~/Views/Partial1.cshtml” with just a simple div

3.添加了一个“~/Views/Partial1.cshtml”,只有一个简单的div

<div>Hello from Partial 1</div>

4.Modified root Web.Config webpages:Enabled to true.

4.修改根Web.Config网页:启用为true。

<add key="webpages:Enabled" value="true" />

5.Added system.web.webPages.razor to root Web.config

5.将system.web.webPages.razor添加到根Web.config

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

6.Added sectionGroup name="system.web.webPages.razor" to configSections of root web.config

6.在根web.config的configSections中增加sectionGroup name="system.web.webPages.razor"

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

7.Set RootView.cshtml as Start Page

7.设置RootView.cshtml为起始页

8.Run and get the following error: "Type 'ASP._Page_RootView_cshtml' does not inherit from 'System.Web.WebPages.WebPage'.

8.运行并得到以下错误:“类型'ASP._Page_RootView_cshtml'不继承自'System.Web.WebPages.WebPage'。

I am at a loss of how to fix this. The Code Camper code works fine. I have compared line by line and see no differences in the code that would prevent from working.

我不知道如何解决这个问题。Code Camper 代码工作正常。我逐行进行了比较,发现代码中没有阻止工作的差异。

Thoughts? Dan

想法?担

回答by ngm

Remove the web.configfrom your Viewsfolder.

web.config从您的Views文件夹中删除。

As you're including Partial1.cshtmlfrom that folder, it is also including the web.config from within there. And that web.config is saying that all pages must inherit from WebViewPage.

当您Partial1.cshtml从该文件夹中包含时,它还包含了其中的 web.config。那个 web.config 是说所有页面都必须从 WebViewPage 继承。

回答by Phil Francis

I am a beginner to this, but one thing I did not realise is that cshtml pages are served through a controller, rather than by loading them directly.

我是初学者,但我没有意识到的一件事是 cshtml 页面是通过控制器提供的,而不是直接加载它们。

In combination with the above, I also had to set the following key to false in the web.config file:

结合上述内容,我还必须在 web.config 文件中将以下键设置为 false:

<add key="webpages:Enabled" value="false" />

回答by Yogurtu

Do not delete the webconfig, is a very important file in your views!!!

不要删除webconfig,在你看来是一个很重要的文件!!!

Instead, do this:

相反,请执行以下操作:

Enable "View all Files" in the web project that is failing, and search for a file that seems correct but is not included in visual studio, and delete it. If it fails in your deployment folder, try to clean the folder as well, and re-deploy the site, you might have unnecesary files that might cause the same problem.

在失败的 web 项目中启用“查看所有文件”,并搜索一个看起来正确但未包含在 Visual Studio 中的文件,然后将其删除。如果它在您的部署文件夹中失败,请尝试清理该文件夹,然后重新部署该站点,您可能有可能导致相同问题的不必要的文件。

In my case, at the root of the webproject I had an extra copy of _ViewStart.cshtml (excluded from the project), I deleted that file, and that did the trick.

就我而言,在 web 项目的根目录下,我有一个额外的 _ViewStart.cshtml 副本(从项目中排除),我删除了该文件,然后就成功了。

Hope it helps, let me know if this solve your problem as well.

希望它有所帮助,让我知道这是否也解决了您的问题。