servicestackservicestack-razor

ServiceStack Razor not rendering pages correctly after upgrade to 4.x


After upgrading the ServiceStack libraries on my website from 3.9.71 to 4.0.33, I noticed that ServiceStack.Razor is no longer rendering pages correctly. It appears to not be reading the layout.cshtml file. The pages load without the layout and without an error or warning. I've tried putting the layout.cshtml file in /Views/_layout.cshtml and /Views/Shared/_layout.cshtml.

In addition to replacing the packages during the upgrade, I also made the necessary changes to the Web.config file. Here is a snippet from my Web.config file. Please let me know if this is helpful or if I need to provide other information. Any help would be appreciated.

<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor"> <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor" requirePermission="false" /> <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor" requirePermission="false" /> </sectionGroup> </configSections> <appSettings> <add key="webPages:Enabled" value="false" /> <add key="servicestack:license" value="{LICENSE_KEY_HERE}" /> </appSettings> <system.webServer> <handlers> <remove name="ChartImageHandler" /> <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> </handlers> <modules> <add name="RightsModule" type="UI.security.RightsHttpModule" /> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </modules> <validation validateIntegratedModeConfiguration="false" /> <rewrite> <rules> <rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true"> <match url="*" /> <conditions> <add input="{HTTP_HOST}" pattern="www.google.com" /> </conditions> <action type="Redirect" url="http://google.com/{R:0}" /> </rule> </rules> </rewrite> </system.webServer> <!-- Required for MONO --> <system.web> <httpRuntime executionTimeout="3600" maxRequestLength="1048576" /> <httpModules> <add name="RightsModule" type="UI.security.RightsHttpModule" /> <add name="Airbrake" type="SharpBrake.NotifierHttpModule, SharpBrake" /> </httpModules> <httpHandlers> <!-- razor --> <add path="*" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" /> <remove path="*.asmx" verb="*" /> <add path="*.asmx" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" /> <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" /> </httpHandlers> <globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" /> <compilation targetFramework="4.5.1" debug="true"> <assemblies> <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" /> <!-- add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/ --> </assemblies> <buildProviders> <add extension=".cshtml" type="ServiceStack.Razor.CSharpRazorBuildProvider, ServiceStack.Razor" /> </buildProviders> </compilation> <authentication mode="Forms"> <forms loginUrl="/optimize/login.cshtml" protection="All" timeout="1440" name="AudiencePoint" path="/app" requireSSL="false" slidingExpiration="true" defaultUrl="/optimize" cookieless="UseCookies" enableCrossAppRedirects="false" /> </authentication> <authorization> <allow users="*" /> </authorization> </system.web> <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc" /> <pages pageBaseType="ServiceStack.Razor.ViewPage"> <namespaces> <add namespace="ServiceStack.Html" /> <add namespace="ServiceStack.Razor" /> <add namespace="ServiceStack.Text" /> <add namespace="ServiceStack.OrmLite" /> <add namespace="UI" /> <add namespace="System" /> <add namespace="ServiceStack" /> </namespaces> </pages> </system.web.webPages.razor> <location path="optimize"> <system.web> <httpHandlers> <add path="*" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" /> </httpHandlers> </system.web> <!-- Required for IIS 7.0 --> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> <validation validateIntegratedModeConfiguration="false" /> <handlers> <add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" /> </handlers> </system.webServer> </location> </configuration>


Solution

  • Looks like the issue was that my code used the ServiceStack.Service.GetSession() method instead of the ServiceStack.Service.SessionAs() method to retrieve the UserSession. Not sure why this worked in 3.9.x but after I changed it, it started rendering in 4.0.x.

    For the difference between the two methods checkout this stackoverflow question. They can't be used interchangeably.