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

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

问题描述

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

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

我已经可以使用Controller和Views,但是无法从该库中加载资源(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/**"
    ]
  },

谁能告诉我怎么了?

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

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天全站免登陆