【问题标题】:ServiceStack Razor (self-hosted) with embedded images/css带有嵌入式图像/css 的 ServiceStack Razor(自托管)
【发布时间】:2013-07-04 17:20:34
【问题描述】:

我有一个使用出色的服务堆栈的自托管 WebService/WebApplication。

我的视图嵌入在 DLL 中,图像也是如此。我正在使用来自 GitHub 的 ResourceVirtualPathProvider 代码。它可以正确找到索引页面和布局,但找不到嵌入的图像/css(可能很明显)。

我将如何配置 Razor 插件以搜索路径提供程序。我已经在调试模式下进行了检查,并且路径提供程序已经找到了所有的 css 和图像。他们只是没有被路由。

编辑

我尝试将 AppHost 的 VirtualPathProvider 属性设置为我配置 RazorFormat 插件时使用的同一提供程序,但无济于事。

最后编辑

感谢 Mythz 的回复,我现在已经完成了这项工作并提供了以下解决方案:

  1. 首先(我以前也有过),我使用来自GitHubEmbedded 代码来创建资源虚拟路径提供程序、目录和文件。

  2. 实现了一个 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
    
  3. 在我的配置函数中配置了所有内容(它是静态的这一事实是我的场景所独有的,但它是从常规 AppHost 的 Configure 函数中有效调用的)

    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
    

【问题讨论】:

    标签: c# .net razor servicestack


    【解决方案1】:

    你看过RazorRockstars的自托管版本吗?

    需要了解静态文件的不是 RazorFormat,它们只是由 ServiceStack 自己处理。

    您需要将每个静态文件的 Build Action 设置为 Copy if newer,以便将它们复制到 bin/ 目录,以便 ServiceStack 可以找到它们因为它是托管自托管版本的 ServiceStack 的基本目录。

    【讨论】:

    • Hi Mythz,我想将它们解析为嵌入式资源,而不是静态文件。视图(主索引和布局)完美解析,而不是嵌入的图像/css - 这是我在这种情况下的偏好:)
    • 该功能是EmbeddedFeature 的一部分,您需要从源代码编译才能使用。注意:您现在可以使用它,但它不是 ServiceStack 的官方部分,因为我们打算为将来的商业功能保留它。
    • 我采用了给出的代码,并提供了两次ResourcePathProvider 类(因为我使用MultiPathProvider。ServiceStack 在我的主 DLL 中找到 index.cshtml 和共享布局在依赖的 DLL 中,但不是图像...还有什么我需要做的吗?
    • 静态文件处理程序不使用虚拟路径提供程序,因此您必须通过注册自己的自定义IAppHost.CatchAllHandlers 来自行处理。
    • @Moo-Juice 只需在您的 AppHost 中注册一个 CatchAllHandler 并调试回调,然后使用它来查看 ResourcePathProvider - 您想要返回一个 IHttpHandler 来完成您需要的工作。 MarkdownFormatRazorFormat 都有注册和使用 CatchAllHandler 的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    相关资源
    最近更新 更多