从外部库提供静态文件 [英] Serve Static Files from External Library

查看:25
本文介绍了从外部库提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to serve static files that was inside an external library.

我已经开始处理控制器和视图,但我无法从该库加载资源(javascript、图像等).

I'm already do to work Controller and Views, but i'm not able to load the resources (javascript, images, ...) from that library.

这是我的 Startup.cs

Here is my 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 设置:

And here my buildOptions settings on project.json of External Library:

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

谁能告诉我出了什么问题?

Can anyone tell me what's wrong?

谢谢大家(抱歉我的英语不好)

Thank you all (and sorry my bad english)

推荐答案

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

I know this is an old question but i faced the same problem and for me the solution was to create the embedded file provider passing as parameters the external assembly and a string like "assemblyName.wwwroot".

假设您的程序集名称是 PersonComponent

Assuming that your assembly name is PersonComponent

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

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

Then you have to use this file provider in UserStaticFiles call

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

可选地,您可以使用替代内容请求路径,这样您就可以使用不同的 URL 获取本地和外部资源.为此,只需在创建 StaticFileOptions

Optionaly you can use an alternative content request path, so you can get your local and external resources using a different URL. To do this just fill RequestPath variable when creating StaticFileOptions

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

这篇关于从外部库提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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