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

查看:80
本文介绍了使用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类:

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具有< GenerateEmbeddedFilesManifest> true</GenerateEmbeddedFilesManifest>< EmbeddedResource Include =" Static \ ** \ *"/> 设置.

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 代码,在这种情况下,所有对嵌入式static的引用文件返回 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天全站免登陆