c# - 带有嵌入式图像/css 的 ServiceStack Razor(自托管)

标签 c# .net razor servicestack

我有一个自托管的 WebService/WebApplication,使用了很棒的服务堆栈。

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

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

编辑

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

最后编辑

感谢 Mythz 的回复,我现在已经开始工作并提供以下解决方案:

  1. 首先(我以前有过),我使用了 GitHub 中的 Embedded 代码创建资源虚拟路径提供程序、目录和文件。

  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
    

最佳答案

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

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

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

关于c# - 带有嵌入式图像/css 的 ServiceStack Razor(自托管),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17475547/

相关文章:

C# 从左到右移动标签

c# - 将字节指针传递给 CLR 函数

c# - 如何在 C# 中将 CSHTML 读取为 XML 文件

c# - 使用 Razor 在同一行上循环html标签

c# - Xml Serializer 中的注释问题

c# - 获取属性时如何忽略继承链?

c# - 本地 IIS 和 Express IIS 之间的 Visual Studio 区别

.net - 小型 .net/php 开发商店的推荐设置是什么?

c# - 使用 group by 时如何对 Linq 中两个日期时间之间的减法时间跨度求和?

asp.net-mvc-3 - 追加两个 IEnumerable 项