ServiceStack Razor files in separate project

149 Views Asked by At

I have a solution consisting of a ServiceStack back-end, with the regular setup (AppHost, ServiceInterface and ServiceModel), and both a winforms app and a iOS app consuming services.

Now I'd like to make a web-admin, and am looking for advice on how to structure this. I'd like to keep the apphost project small, as SS docs say

Ideally the root-level AppHost project should be kept lightweight and implementation-free.

So I'd like to have the web-admin in a separate project, with all the .cshtml and content and it all.

Is this possible? Not recommended? Any ideas?

Some alternatives I can think of are

  • single-page-app, let the few cshtml files live in the AppHost-project. Is this worth the learning curve?
  • have the separate project call the web-services. Wouldn't that be very ineffective, considering that they live on the same web-server? Or should it be considered an advantage, since it makes everything loosely coupled?
2

There are 2 best solutions below

3
On

Look at the docs on using Compiled Razor Views and the ServiceStack.Gap GitHub repository on how to create embedded ServiceStack solutions that utilizes Compiled Razor Views.

0
On

I'd like to provide a more complete answer for beginners:

  1. The project for the web-site must be razor enabled. It must have a reference to ServiceStack.Razor for example, so the .cshtml will compile.

  2. As pointed out by @mythz, add the ServiceStack.Razor.BuildTask to precompile the razor files. This is necessary to get them included the LoadFromAssemblies parameter

  3. In the main AppHost, specify that Razor files are to be found in the other assembly with LoadFromAssemblies e.g:

        this.Plugins.Add(new RazorFormat { LoadFromAssemblies = { typeof(ActivityServer.Admin.AdminServices).Assembly } });
    
  4. So far it's been all about the Razor .cshtml files. To also include services, e.g. for more complex views, just add this to the AppHost() : base(...), or if you want a prefix to those services use AppHost.RegisterService.

One thing to point out is that there is no separate namespace for Razor files in the external DLL, so a "hello.cshtml" in the external DLL collides with a file with the same name in the main AppHost. By putting all cshtml files (in the external dll) under a sub-folder, that will help, because the folder will be part of the URL.