web-dev-qa-db-ja.com

Katana / Owinセルフホストアプリのデフォルトの静的Webページを設定するにはどうすればよいですか?

Owinセルフホストコンソールアプリを使用してWebサイトをセットアップしました。問題なく静的ファイルを提供しています。サイトの静的部分の「ルート」は正しく機能し、WebAPIルートも正常に機能します。

私が閲覧した場合:

http://localhost/index.html

それは私が期待するようなすべてを提示します。しかし、私はそれを設定する方法を理解していません。

http://localhost

index.htmlを(デフォルトのビューとして)表示します。これはIISスタイルのサイトで動作します。 Owinセルフホストで動作させるにはどうすればよいですか?

16
wilee

私はそれをこのようにします:

var physicalFileSystem = new PhysicalFileSystem(webPath);
var options = new FileServerOptions
                          {
                              EnableDefaultFiles = true,
                              FileSystem = physicalFileSystem
                          };
        options.StaticFileOptions.FileSystem = physicalFileSystem;
        options.StaticFileOptions.ServeUnknownFileTypes = true;
        options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" };
        appBuilder.UseFileServer(options);
19
fra

fraの答えのより詳細なバージョン:

1- NuGetでMicrosoft.Owin.StaticFilesをインストールします(NuGet経由でMicrosoft.AspNet.WebApi.OwinSelfHostを既にインストールしていると仮定しました)

2-(Visual Studioで)ソリューションに単一のディレクトリを作成し、その中にすべてのクライアントファイルを配置します。

+Web

--+images

--+pages

------page1

------page2

--+scripts

--+css

---index.html

注:他のすべてのディレクトリを含むルートディレクトリ(web)と、ルートの直下にあるindex.htmlがあります。

3-ここで、Web APIルーティング構成を含む同じクラスに、次のコードを追加します。

var physicalFileSystem = new PhysicalFileSystem(@".\Web"); //. = root, Web = your physical directory that contains all other static content, see prev step
var options = new FileServerOptions
{
    EnableDefaultFiles = true,
    FileSystem = physicalFileSystem
 };
 options.StaticFileOptions.FileSystem = physicalFileSystem;
 options.StaticFileOptions.ServeUnknownFileTypes = true;
 options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" }; //put whatever default pages you like here
 appBuilder.UseFileServer(options);

4-前のコードを機能させるためのもう1つのステップ:Webディレクトリ(およびすべてのネストされたディレクトリ)内のすべてのファイルのCopy to output directoryプロパティがCopy Always [ファイルを選択| F4キーを押すか、右クリックしてプロパティ| Copy to output directory]に移動します

それで全部です :)

21
Sameh Deabes

遅い答えかもしれませんが、デフォルトのドキュメントだけが必要な場合は、機能するコードが少なくなります。

builder.UseDefaultFiles(new DefaultFilesOptions
{
    DefaultFileNames = Enumerable.Repeat("index.html", 1).ToList()
});

何らかの理由でbuilder.UseStaticFilesの前に呼び出す必要があります。

Microsoft.Owin.StaticFilesのバージョンは3.0.1です

7
the_joric

多分これ 遅い 答えは他の助けになるかもしれません:)私はSelfHostOwinアプリで同じ問題を抱えていました。

私が見つけた解決策は、PhysicalFileSystemクラスをカプセル化するIFileSystemインターフェイスからクラスを実装することです(これもIFileSystemから実装されます)。

public class WebPhysicalFileSystem : IFileSystem
{
    private PhysicalFileSystem InnerFileSystem { get; set; }
    private string Default { get; set; }

    public WebPhysicalFileSystem(string root, string defaultFile = "index.html")
    {
        InnerFileSystem = new PhysicalFileSystem(root);
        Default = defaultFile;
    }

    public bool TryGetDirectoryContents(string subpath, out IEnumerable<IFileInfo> contents)
    {
        if(InnerFileSystem.TryGetDirectoryContents(subpath, out contents))
        {
            return true;
        }

        string defaultPath = System.IO.Path.Combine(InnerFileSystem.Root, Default);
        return InnerFileSystem.TryGetDirectoryContents(defaultPath, out contents);
    }

    public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
    {
        if (InnerFileSystem.TryGetFileInfo(subpath, out fileInfo))
        {
            return true;
        }

        string defaultPath = System.IO.Path.Combine(InnerFileSystem.Root, Default);
        return InnerFileSystem.TryGetFileInfo(defaultPath, out fileInfo);
    }
}

そしてアプリで:

var options = new FileServerOptions
{
    EnableDefaultFiles = true,
    FileSystem = new WebPhysicalFileSystem("yourRoot");
};
3
SkwalieGator

私の場合、私は行方不明でした

<handlers>
    <add name="AspNetStaticFileHandler" path="*" verb="*" type="System.Web.StaticFileHandler" />
</handlers>

私のweb.configのsystem.webServer部分。

1
Cubelaster