ServiceStack Razor files in separate project

半城伤御伤魂 提交于 2019-12-11 13:32:29

问题


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?

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/30398431/servicestack-razor-files-in-separate-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!