Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceStack Razor Views not compiling

I have successfully implement Razor Viewpages in a selfhosted service, the pages rendered perfectly until I updated to 3.9.56. The views were tested in a windows forms application along with a Windows Forms client that would consume json responses from the service. This still works perfectly, but now when I test the Razor Views I keep getting the following exception when requesting a page:

ERROR: Error occured while Processing Request: [HttpCompileException] c:\Users\Cornel\AppData\Local\Temp\2msjdedu.0.cs(24): error CS0246: The type or namespace name 'ViewPage' could not be found (are you missing a using directive or an assembly reference?), Exception: c:\Users\Cornel\AppData\Local\Temp\2msjdedu.0.cs(24): error CS0246: The type or namespace name 'ViewPage' could not be found (are you missing a using directive or an assembly reference?)

I built a small console application to host the service and the Razor Views and the pages render correctly. Both projects have the same references, exept for the Windows Forms assembly references on the test framework. Both projects start the AppHost from the same management class in a seperate assembly and all Views have been published.

The only difference in the config file is a <userSettings> section on the Windows Forms test framework.

[Edit]

When I change @inherits ViewPage to @inherits ServiceStack.Razor.ViewPage everything works as expected

like image 561
cornelha Avatar asked Aug 02 '13 06:08

cornelha


1 Answers

This problem was driving me crazy. I just found the solution: The app.config of the ServiceStack.Razor application contains a section like this:

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

These are the default namespace imports for all Razor templates. The problem occurs when the application is in a separate DLL, like it seems is the case with your example too. Let's say you have a console application project called MyApp.Host, which references MyApp. In stead of looking for the default imports in MyApp.Host.exe.config, ServiceStack looks for them in the config file of the DLL containing the AppHost, in this case MyApp.dll.config. The problem is, this config file isn't automatically copied into the bin folder of MyApp.Host when building.

The solution is relatively simple: Add something similar to the following to the post build events of MyApp.Host:

copy /y $(SolutionDir)MyApp\app.config $(TargetDir)MyApp.dll.config

Edit: You can also add the imports programmatically (see Getting a HttpCompileException in ServiceStack Razor view (Self hosted)):

EndpointHostConfig.RazorNamespaces.Add("ServiceStack.Razor");
EndpointHostConfig.RazorNamespaces.Add("MyApp");
EndpointHostConfig.RazorNamespaces.Add("MyApp.Services");
SetConfig(new EndpointHostConfig
{
    DefaultRedirectPath = "/Home",
});
like image 77
dabide Avatar answered Sep 21 '22 07:09

dabide