我目前在让 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>
智能感知不会为 default.cshtml 中的“@inherits ViewPage”行中的“ViewPage”条目着色当我将该行更改为“@inherits ServiceStack.Razor.ViewPage”时,智能感知会为 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”网址下呈现得很好。Views 文件夹中没有 web.config 文件。