Razor视图类型不继承自'System.Web.WebPages.WebPage'

时间:2012-05-05 10:21:32

标签: asp.net-mvc-3 webforms webforms-view-engine webforms-routing

我在配置asp.net mvc应用程序时遇到问题:

[HttpException (0x80004005): Type 'ASP._Page_Currency_Index_cshtml' does not inherit from 'System.Web.WebPages.WebPage'.]
System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp) +11454171
System.Web.WebPages.BuildManagerWrapper.CreateInstanceOfType(String virtualPath) +347
System.Web.WebPages.VirtualPathFactoryManager.CreateInstanceOfType(String virtualPath) +239
System.Web.WebPages.WebPageHttpHandler.CreateFromVirtualPath(String virtualPath, IVirtualPathFactory virtualPathFactory) +57
System.Web.WebPages.WebPageRoute.DoPostResolveRequestCache(HttpContextBase context) +407
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +270

我想要做的步骤:

  • 将带有视图的控制器移动到单独的库中,并将dll放入bin文件夹。
  • 创建虚拟文件提供程序和虚拟文件以从程序集中获取数据:

    public override Stream Open()
    {
        var resourceName = this.path;
        var asmName = this.assembly.GetName().Name;
        var manifestName = asmName + ".Views" + resourceName.Trim('~').Replace("/", ".");
        var resourceStream = this.assembly.GetManifestResourceStream(manifestName);
        return resourceStream;
    }
    
  • 将此行添加到web.config中:

    <configSections>
      <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>
    </configSections>
    <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.Helpers" />
          <add namespace="System.Web.Mvc" />
          <add namespace="System.Web.Mvc.Ajax" />
          <add namespace="System.Web.Mvc.Html" />
          <add namespace="System.Web.Routing" />
          <add namespace="System.Web.WebPages" />
        </namespaces>
      </pages>
    </system.web.webPages.razor>
    
  • 当用户访问此控制器的页面时,它会正确编译到以下类中:

    public class _Page_Currency_Index_cshtml : System.Web.Mvc.WebViewPage<GridModel<CurrencyModel>> {
    
  • 之后它失败并出现此错误(正如我所见 - 他是对的,Type'ASP._Page_Currency_Index_cshtml'不会继承自'System.Web.WebPages.WebPage'。)

我应该怎么做才能让HttpApplication知道这个页面应该作为System.Web.Mvc.WebViewPage而不是System.Web.WebPages.WebPage来管理?

我试图将Views / Web.Config移动到bin文件夹中,但它没有帮助。

1 个答案:

答案 0 :(得分:4)

尝试将其添加到AppSettings中的Web.Config

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