尝试在站点根目录使用带有cshrml的ServiceStack Razor插件时出现编译错误

我目前在使用ServiceStack Razor在网站的根目录下呈现我的页面时遇到了一些问题.我遇到以下错误

编译器错误消息:CS0246:找不到类型或命名空间名称“ViewPage”(您是否缺少using指令或程序集引用?)
public class @__CompiledTemplate:ViewPage {

我刚开始在网站上,这里是web.config和剃刀页面的内容

这是网站根目录下的web.config文件

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <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>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
      <buildProviders>
        <add extension=".cshtml" type="ServiceStack.Razor.CSharpRazorBuildProvider, ServiceStack.Razor" />
      </buildProviders>
    </compilation>
      <httpRuntime targetFramework="4.5" />
    <httpHandlers>
      <add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*"/>
    </httpHandlers>
  </system.web>

  <!-- Required for IIS 7.0 (and above?) -->
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
    </handlers>
  </system.webServer>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="ServiceStack.Razor.ViewPage">
      <namespaces>
        <add namespace="ServiceStack.Html" />
        <add namespace="ServiceStack.Razor" />
        <add namespace="ServiceStack.Text" />
        <add namespace="ServiceStack.OrmLite" />
        <add namespace="FERNSWeb" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

</configuration>

这是站点根目录下的default.cshtml文件

@inherits ViewPage
This is the body

以及网站根目录下的_Layout.cshtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>FERNS - @ViewBag.Title</title>
</head>
<body>
    @RenderBody()
</body>
</html>

intellisense不会将default.cshtml中“@inherits ViewPage”行中的“ViewPage”条目着色
当我将行更改为“@inherits ServiceStack.Razor.ViewPage”时,intellisense会对ViewPage条目进行着色,但这次我得到一个异常,而不是编译错误.

异常详细信息:System.InvalidCastException:无法将类型为“Razor .__ CompiledTemplate”的对象强制转换为类型“System.Web.IHttpHandler”.

[InvalidCastException:无法将类型为’Razor .__ CompiledTemplate’的对象强制转换为’System.Web.IHttpHandler’.]
   System.Web.WebPages.WebPageHttpHandler.CreateFromVirtualPath(String virtualPath,VirtualPathFactoryManager virtualPathFactoryManager)56
   System.Web.WebPages.WebPageRoute.DoPostResolveRequestCache(HttpContextBase context)264
   System.Web.WebPages.WebPageHttpModule.OnApplicationPostResolveRequestCache(Object sender,EventArgs e)89
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()136
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean& completedSynchronously)69

奇怪的是,如果我移动Default.cshtml和_Layout.cshtml文件我创建的用于测试的Views文件夹,页面在“/ views”URL下呈现得很好. Views文件夹中没有web.config文件.

最佳答案 刚刚解决了我上面遇到的问题.我必须将以下内容添加到根web.config文件中才能使其正常工作

  <appSettings>
    <add key="webPages:Enabled" value="false" />
  </appSettings>

不确定我完全理解为什么.对于web下的web.config和文件夹的web.config,“webPages:Enabled”的默认值是否不同?这是我能想到的唯一解释.

点赞