【问题标题】:Serve Static Files from External Library从外部库提供静态文件
【发布时间】:2017-05-18 23:21:38
【问题描述】:

我试图提供外部库中的静态文件。

我已经完成了控制器和视图的工作,但我无法从该库中加载资源(javascript、图像...)。

这是我的 Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
    //...
    var personAssembly = typeof(PersonComponent.Program).GetTypeInfo().Assembly;
    var personEmbeddedFileProvider = new EmbeddedFileProvider(
          personAssembly,
          "PersonComponent"
       );

     services
       .AddMvc()
       .AddApplicationPart(personAssembly)
       .AddControllersAsServices();

    services.Configure<RazorViewEngineOptions>(options =>
                {
                    options.FileProviders.Add(personEmbeddedFileProvider);
                });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        //...
        var personAssembly = typeof(PersonComponent.Program).GetTypeInfo().Assembly;
        var personEmbeddedFileProvider = new EmbeddedFileProvider(
            personAssembly,
            "PersonComponent"
        );

        app.UseStaticFiles();
        //This not work
        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new CompositeFileProvider(
                personEmbeddedFileProvider
            )
        });
    }

这里是我在外部库的 project.json 上的 buildOptions 设置:

"buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
    "embed": [
      "Views/**/*.cshtml",
      "wwwroot/**"
    ]
  },

谁能告诉我怎么了?

谢谢大家(对不起我的英语不好)

【问题讨论】:

  • 可能是因为您的资源位于 wwwroot 文件夹中。尝试将其内容移动到外部库的根目录
  • 这对我有用!谢谢!但我仍在寻找替代方案而不将内容移动到根目录,因为我想将 Bower 用于外部插件。我再次发布的任何内容
  • 我认为不可能,无需编写您自己的提供程序。 EmbeddedFileProvider 似乎不支持看起来像github.com/aspnet/FileSystem/blob/rel/1.1.0/src/… 的根路径 这个根路径是为PhysicalFileProvider 设置为wwwroot,因为请求中间件不知道文件所在的位置。使用中间件时,只有不带wwwroot 的路径会传递给中间件。
  • 你可以尝试使用命名空间重载的构造函数重载,但我怀疑它会起作用并且可能会破坏视图
  • 无论如何将包存储在类库中也是一个坏主意。通常,您在部署后或创建 docker 容器时使用 npm 或 bower 来恢复它

标签: c# .net asp.net-core asp.net-core-mvc .net-core


【解决方案1】:

我知道这是一个老问题,但我遇到了同样的问题,对我来说,解决方案是创建嵌入式文件提供程序,将外部程序集和字符串作为参数传递,如 "assemblyName.wwwroot" .

假设您的程序集名称是 PersonComponent

    var personAssembly = typeof(PersonComponent.Program).GetTypeInfo().Assembly;
    var personEmbeddedFileProvider = new EmbeddedFileProvider(
        personAssembly,
        "PersonComponent.wwwroot"
    );

那么你必须在 UserStaticFiles 调用中使用这个文件提供程序

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = personEmbeddedFileProvider
});

您可以选择使用其他内容请求路径,这样您就可以使用不同的 URL 获取本地和外部资源。 为此,只需在创建 StaticFileOptions 时填写 RequestPath 变量

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = personEmbeddedFileProvider,
    RequestPath = new PathString("/external")
});

【讨论】:

  • 外部程序集PersonComponent的静态文件夹叫什么名字? '\wwwroot' 或 '\external'
  • 文件夹的名称是 wwwrootexternal 是一个路径字符串,如果您在不同的项目中有同名的静态文件,您应该使用它来避免冲突。例如,如果您的站点项目和 PersonComponent 中有 style.css,您应该使用 localhost/style.css 访问第一个,使用路径字符串 localhost/external/style.css访问第二个>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
  • 2012-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多