简体   繁体   English

从外部库服务静态文件

[英]Serve Static Files from External Library

I was trying to serve static files that was inside an external library. 我试图提供外部库中的静态文件。

I'm already do to work Controller and Views, but i'm not able to load the resources (javascript, images, ...) from that library. 我已经可以使用Controller和Views,但无法从该库中加载资源(javascript,图像等)。

Here is my Startup.cs 这是我的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
            )
        });
    }

And here my buildOptions settings on project.json of External Library: 这是我在外部库的project.json上的buildOptions设置:

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

Can anyone tell me what's wrong? 谁能告诉我怎么了?

Thank you all (and sorry my bad english) 谢谢大家(对不起我的英语不好)

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" . 我知道这是一个老问题,但是我遇到了同样的问题,对我来说,解决方案是创建嵌入式文件提供程序,将外部程序集和诸如“ assemblyName.wwwroot”之类的字符串作为参数传递。

Assuming that your assembly name is PersonComponent 假设您的程序集名称为PersonComponent

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

Then you have to use this file provider in UserStaticFiles call 然后,您必须在UserStaticFiles调用中使用此文件提供程序

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

Optionaly you can use an alternative content request path, so you can get your local and external resources using a different URL. (可选)您可以使用替代内容请求路径,以便可以使用其他URL获取本地和外部资源。 To do this just fill RequestPath variable when creating StaticFileOptions 为此,只需在创建StaticFileOptions时填充RequestPath变量

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM