使用 StaticFileOptions() 破坏 Razor 类库中嵌入的静态文件 [英] Using StaticFileOptions() breaks embedded static files from Razor Class Library

查看:34
本文介绍了使用 StaticFileOptions() 破坏 Razor 类库中嵌入的静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 StaticFileOptions

从各种 SO 和其他文章中,您可以这样做:

From various SO and other articles, this is how you do it:

app.UseStaticFiles(new StaticFileOptions
{
    OnPrepareResponse = ctx =>
    {
        const int durationInSeconds = 60 * 60 * 24;
        ctx.Context.Response.Headers[HeaderNames.CacheControl] =
        "public,max-age=" + durationInSeconds;
    }
}); 

但是,我使用了 RCL 提供的一堆静态文件.RCL 有一个我在这篇文章中使用的 StaticServing.cs 类:Razor 类库也可以打包静态文件(js、css 等)吗?

However, I am using a bunch of static files provided by a RCL. The RCL has a StaticServing.cs class which I have used from this article: Can Razor Class Library pack static files (js, css etc) too?

为了我的问题的完整性,这个类如下:

For the sake of completeness to my question, this class is as follows:

public StaticServing(IHostingEnvironment environment)
    {
        Environment = environment;
    }
    public IHostingEnvironment Environment { get; }

    public void PostConfigure(string name, StaticFileOptions options)
    {
        name = name ?? throw new ArgumentNullException(nameof(name));
        options = options ?? throw new ArgumentNullException(nameof(options));

        // Basic initialization in case the options weren't initialized by any other component
        options.ContentTypeProvider = options.ContentTypeProvider ?? new FileExtensionContentTypeProvider();
        if (options.FileProvider == null && Environment.WebRootFileProvider == null)
        {
            throw new InvalidOperationException("Missing FileProvider.");
        }

        options.FileProvider = options.FileProvider ?? Environment.WebRootFileProvider; 

        string basePath = "Static";

        ManifestEmbeddedFileProvider filesProvider = new ManifestEmbeddedFileProvider(GetType().Assembly, basePath);
        options.FileProvider = new CompositeFileProvider(options.FileProvider, filesProvider);
    }
}

在消费项目 startup.cs 我有 services.ConfigureOptions(typeof(StaticServing)); 并且 RCL 有 true</GenerateEmbeddedFilesManifest> 设置.

in the consuming projects startup.cs I have services.ConfigureOptions(typeof(StaticServing)); and the RCL has <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest> and <EmbeddedResource Include="Static***" /> settings.

有了这一切,一切正常... 除非我在问题的开头添加了 StaticFileOptions 代码,在这种情况下,所有对嵌入式静态的引用文件返回 404.

With this all in place everything works... UNLESS I add the StaticFileOptions code at the start of the question, in which case, all references to the embedded static files return 404.

我尝试添加:

  • FileProvider = env.ContentRootFileProvider
  • FileProvider = env.WebRootFileProvider

StaticFileOptions 设置,但这不起作用.

To the StaticFileOptions setting, but that isn't working.

推荐答案

静态文件中间件可以通过以下两种方式之一接收其StaticFileOptions:

The static files middleware can receive its StaticFileOptions in one of two ways:

  1. 通过依赖注入隐式实现.
  2. 明确地通过调用 UseStaticFiles.

在您的问题场景中,您(无意中)尝试使用这两种方法配置中间件.只要向 UseStaticFiles 调用添加参数,您就替换框架提供的中间件配置,其中包括 RCL 支持设置.

In your problematic scenario, you're (inadvertently) attempting to configure the middleware using both of these approaches. As soon as you add an argument to the UseStaticFiles call, you're replacing the framework-provided configuration for the middleware, which includes setup for RCL support.

要构建框架提供的配置,您可以利用 ConfigureServices 中的选项模式:

To build on the framework-provided configuration, you can take advantage of the options pattern in ConfigureServices:

services.Configure<StaticFileOptions>(options =>
{
    options.OnPrepareResponse = ctx =>
    {
        const int durationInSeconds = 60 * 60 * 24;
        ctx.Context.Response.Headers[HeaderNames.CacheControl] =
            "public, max-age=" + durationInSeconds;
    }  
});

您还需要删除传递给 UseStaticFiles 的参数.

You'll also need to remove the argument being passed to UseStaticFiles.

这篇关于使用 StaticFileOptions() 破坏 Razor 类库中嵌入的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆