2017-01-04 5 views
1

外部のライブラリの中にある静的なファイルを処理しようとしていました。外部ライブラリの静的ファイルを提供する

私はすでにControllerとViewsを操作していますが、そのライブラリからリソース(javascript、images、...)をロードできません。ここで

は私Startup.csある外部ライブラリのproject.jsonに

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 
      ) 
     }); 
    } 

そして、ここに私のbuildOptions設定:

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

誰もが間違って何を教えてもらえますか?

はあなたのすべてをありがとう(と申し訳ありません私の英語の悪い)

+1

を作成すると、あなたのリソースがwwwrootフォルダ内にあることを、事実にに起因することができるときRequestPath変数を埋めます。そのコンテンツを外部のライブラリのルートに移動してみてください – Tseng

+0

それは私のために働いています!ありがとう! しかし、私は外部のプラグインにBowerを使用したいので、ルートにコンテンツを移動させずに代替案を探しています。 私が再び投稿するもの –

+0

私は、あなた自身のプロバイダを書かずに、その可能性は考えていません。 'EmbeddedFileProvider'は、https://github.com/aspnet/FileSystem/blob/rel/1.1.0/src/Microsoft.Extensions.FileProviders.Physical/PhysicalFileProvider.cs#のようなルートパスをサポートしていないようです。 L39このルートパスは 'PhysicalFileProvider'を' wwwroot'に設定します。なぜなら、リクエストミドルウェアはファイルがどこにあるのか分からないからです。ミドルウェアを使用すると、 'wwwroot'なしのパスのみがミドルウェアに渡されます。 – Tseng

答えて

3

私はこれが古い質問です知っているが、私は解決策は、外部アセンブリをパラメータとして渡す埋め込みファイルプロバイダを作成することでした、同じ問題に直面し、私のためにと「assemblyName.wwwroot」のような文字列。

あなたのアセンブリ名がPersonComponent

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

であると仮定すると、あなたはOptionalyあなたが代替コンテンツ要求のパスを使用することができますUserStaticFilesコール

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

でこのファイルプロバイダを使用する必要があり、別のURLを使用してローカルリソースと外部リソースを取得することができます。これを行うには だけStaticFileOptions

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

外部アセンブリPersonComponentの静的フォルダの名前は何ですか? '\ wwwroot'または '\ external' – Jesus

+1

フォルダの名前は** wwwroot **です。 ** external **は、異なるプロジェクトに同じ名前の静的ファイルがある場合の衝突を避けるために使用するパス文字列です。 たとえば、サイトプロジェクトとPersonComponentにstyle.cssがある場合、http://localhost/style.css**を使用して最初のものにアクセスし、パス文字列** http:///localhost/external/style.css** –

関連する問題