ServiceStack Razor (stand alone) with embedded images / css

I have a standalone WebService / WebApplication using the wonderful Service Stack.

My views are embedded in DLLs as well as images. I am using ResourceVirtualPathProvider code from GitHub. It finds the index page and layouts correctly, but cannot find the embedded images / css (perhaps obviously).

How do I configure the Razor plugin to search for path providers. I checked debug mode and path providers found all css and images. They just don't get routing.

EDIT

I tried to set the AppHost VirtualPathProvider property to the same provider with which I configured the RazorFormat plugin, but to no avail.

LAST EDIT

Thanks to Mythz's answer, I now got this working and provided the solution below:

  • Firstly (and I had it before), I used the Embedded code from GitHub to create a virtual resource path for suppliers, directories and files.

  • Implemented by VirtualFileHandler:

     public sealed class VirtualFileHandler : IHttpHandler, IServiceStackHttpHandler { private IVirtualFile _file; /// <summary> /// Constructor /// </summary> /// <param name="file">File to serve up</param> public VirtualFileHandler(IVirtualFile file) { _file = file.ThrowIfDefault("file"); } // eo ctor public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { ProcessRequest(new HttpRequestWrapper(null, context.Request), new HttpResponseWrapper(context.Response), null); } // eo ProcessRequest public void ProcessRequest(IHttpRequest request, IHttpResponse response, string operationName) { try { response.ContentType = ALHEnvironment.GetMimeType(_file.Extension); using (Stream reader = _file.OpenRead()) { byte[] data = reader.ReadFully(); response.SetContentLength(data.Length); response.OutputStream.Write(data, 0, data.Length); response.OutputStream.Flush(); } } catch (System.Net.HttpListenerException ex) { //Error: 1229 is "An operation was attempted on a nonexistent network connection" //This exception occures when http stream is terminated by the web browser. if (ex.ErrorCode == 1229) return; throw; } } // eo ProcessRequest } // eo class VirtualFileHandler 
  • Configure everything in my configuration function (the fact that it is static is unique to my script, but it is effectively called from the regular AppHost Configure function)

     protected static void Configure(WebHostConfiguration config) { _pathProvider = new MultiVirtualPathProvider(config.AppHost, new ResourceVirtualPathProvider(config.AppHost, WebServiceContextBase.Instance.GetType()), new ResourceVirtualPathProvider(config.AppHost, typeof(ResourceVirtualPathProvider))); config.Plugins.Add(new RazorFormat() { EnableLiveReload = false, VirtualPathProvider = _pathProvider }); /* * We need to be able to locate other embedded resources other than views, such as CSS, javascript files, * and images. To do this, we implement a CatchAllHandler and locate the resource ourselves */ config.AppHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) => { IVirtualFile file = _pathProvider.GetFile(pathInfo); if (file == null) return null; return new VirtualFileHandler(file); }); } // eo Configure 
+3
source share
1 answer

Have you watched your own version of RazorRockstars ?

This is not RazorFormat, which should know about static files, they are simply processed by ServiceStack itself.

You need to set the Build action of each of the static files to Copy, if new , so that they are copied to the bin/ directory so that ServiceStack can find them since this is the base directory in which the own version of ServiceStack is located.

+1
source

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


All Articles