我目前遇到一些问题,让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&amp; completedSynchronously)+69
奇怪的是,如果我将Default.cshtml和_Layout.cshtml文件移动到我为测试创建的Views文件夹,页面在“/ views”URL下呈现正常。 Views文件夹中没有web.config文件。
答案 0 :(得分:2)
刚刚解决了我上面遇到的问题。我必须将以下内容添加到根web.config文件中才能使其正常工作
<appSettings>
<add key="webPages:Enabled" value="false" />
</appSettings>
不确定我完全理解为什么。对于web下的web.config和文件夹的web.config,“webPages:Enabled”的默认值是否不同?这是我能想到的唯一解释。