Compilation errors when trying to use the ServiceStack Razor plugin with cshrml in the root of the site

I am currently having problems getting ServiceStack Razor to render my page in the root of the site. I meet the following error:

Compiler Error Message: CS0246: Could not find name of type or namespace "ViewPage" (do you miss using directive or assembly reference?) Public class @__CompiledTemplate: ViewPage {

I just started on the site and here are the contents of web.config and razor pages

Here is the web.config file in the root directory of the website

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

Here is the default.cshtml file in the root of the site

 @inherits ViewPage This is the body 

and _Layout.cshtml in the root of the site

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

Intellisense does not color the "ViewPage" entry in the "@inherits ViewPage" line in default.cshtml and when I change the line to "@inherits ServiceStack.Razor.ViewPage", intellisense colors the ViewPage record, but this time I get an exception, not an error compilation.

Exception Details: System.InvalidCastException: Cannot reset an object of type "Razor .__ CompiledTemplate" to enter "System.Web.IHttpHandler".

[InvalidCastException: Unable to throw an object of type "Razor .__ CompiledTemplate" to enter "System.Web.IHttpHandler".] System.Web.WebPages.WebPageHttpHandler.CreateFromVirtualPath (String virtualPath, VirtualPathFactoryManager virtualPathWebP.WebPWWFB.WPFWW.FebWW.FebW.FebWW.FebWW.FebWWBFFWWF.WebFFWWF.WebFWWFW.WbFWWF + FEBWWBFWWFBEMWBEBWFWW + FEBWWBFWbEMWEbFWWbFeWB + FEbFWBwFebemWEbPerWeb + FramesWebPageWeb.WebPageWeb.WebPageWeb.WebPageWeb.WebPageWeb. 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 (step IExecutionStep, Boolean & completed synchronously) +69

The strange part: if I dragged the Default.cshtml and _Layout.cshtml files into the Views folder created for testing, the page will simply appear under the heading "/ views". There is no web.config file in the Views folder.

+4
source share
1 answer

Just solved the problem that I encountered. I had to add the following to the root web.config file:

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

Not sure if I fully understand why. Is the default value for "webPages: Enabled" different for web.config as root and web.config for folders? This is the only explanation I could come up with for this.

+2
source

Source: https://habr.com/ru/post/1493313/


All Articles