2017-05-25 9 views
4

既存のWeb APIプロジェクトがあり、ルートから2つのクライアントを提供したい(異なるサブドメインを介してアクセスする)。 Web APIをセットアップして、2つのWebサイトとルートにファイルを提供する機能を実行する正しい方法は何ですか?Web APIからのファイルとルートの提供

  1. 使用ケストレルファイルサーバとして:

    は、私は2つの主要なオプションを参照してください。 Kestrelがルートから2つのサイトを提供するように設定する方法がわかりません(異なるサブドメインから来ています)。それは、最小限の構成がそのまま公開されているようですが、これはサブドメインに基づいて2つのサイトを提供するために拡張可能ではないようです。

    var host = new WebHostBuilder() 
        .UseKestrel() 
        .UseWebRoot("wwwroot") 
        .UseIISIntegration() 
        .UseStartup<Startup>() 
        .Build(); 
    
    host.Run(); 
    

    さらに、私は現在、ケストレルとWeb APIとの統合に問題があります。StackOverflow questionを参照してください。

  2. ルートを担当するWeb APIコントローラーからのサービス・ファイル。これによりURLが読み込まれ、適切なindex.htmlが返されます。そこから、js/app.js、css/core.css、assets/*など、index.htmlが参照する他のファイルがどのように役立つかはわかりません。個々のコントローラはこれらを処理することができますが、それは壊れやすいようです。

どのアプローチが適切ですか?それとも私はこの目的を達成するために考えていない何か他のものがありますか? FedericoDipumaさんのコメント@を1として

+3

あなたはASP.NETコア(ケストレルを使用して)とWebをミックスしたい、なぜ私が理解していないことです同じプロジェクト内のAPI(System.Web)。彼らのアーキテクチャはまったく異なります(私は互換性がないと思います)。 Owin [ネイティブルーティング機能](https://stackoverflow.com/a/25405782/3670737)を使用し、[app.UseStaticFiles()](https://msdn.microsoft.com)のような形式の静的ファイルを提供することを検討することもできます/en-us/library/owin.staticfileextensions.usestaticfiles(v=vs.113).aspx)。 –

+1

私はOWINを使用して終了し、私のニーズにうまくいきました。前のコメントのリンクの情報を組み合わせることで、ソリューションの実装に直接つながりました。ありがとう@FedericoDipuma! –

答えて

1

、私は次のようStartup.csでOWINを使用して終了:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using Owin; 
using Microsoft.Owin.FileSystems; 
using Microsoft.Owin.StaticFiles; 
using System.IO; 

namespace SealingServer 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.MapWhen(ctx => ctx.Request.Headers.Get("Host").Equals("subdomain1.site.com"), app2 => 
      { 
       var firstClientRoot = Path.Combine("./firstClient/"); 
       var firstClientFileSystem = new PhysicalFileSystem(firstClientRoot); 

       var fileServerOptions = new FileServerOptions(); 
       fileServerOptions.EnableDefaultFiles = true; 
       fileServerOptions.FileSystem = firstClientFileSystem; 
       fileServerOptions.DefaultFilesOptions.DefaultFileNames = new[] {"home.html"}; 
       fileServerOptions.StaticFileOptions.OnPrepareResponse = staticFileResponseContext => 
       { 
        staticFileResponseContext.OwinContext.Response.Headers.Add("Cache-Control", new[] { "public", "max-age=0" }); 
       }; 

       app2.UseFileServer(fileServerOptions); 
      }); 
      app.MapWhen(ctx => ctx.Request.Headers.Get("Host").Equals("subdomain2.site.com"), app2 => 
      { 
       var secondClientRoot = Path.Combine("./secondClient/"); 
       var secondClientFileSystem = new PhysicalFileSystem(secondClientRoot); 

       var fileServerOptions = new FileServerOptions(); 
       fileServerOptions.EnableDefaultFiles = true; 
       fileServerOptions.FileSystem = secondClientFileSystem; 
       fileServerOptions.DefaultFilesOptions.DefaultFileNames = new[] { "home.html" }; 
       fileServerOptions.StaticFileOptions.OnPrepareResponse = staticFileResponseContext => 
       { 
        staticFileResponseContext.OwinContext.Response.Headers.Add("Cache-Control", new[] { "public", "max-age=0" }); 
       }; 

       app2.UseFileServer(fileServerOptions); 
      }); 
     } 
    } 
} 
関連する問題