Asp.net-Web-Api

如何使用 OWIN 自託管的 web api 提供 index.html

  • February 12, 2015

應該是一個簡單的問題,只是找不到答案。

我有一個帶有 web api 的 SPA (AngularJS),它是由 Owin 自託管的。我使用 Nancy 來提供頁面,但我想擺脫 Nancy 並使用 Index.html 作為我的單個頁面。

我在這裡看到了這個問題:如何將 Web API 以外的所有內容路由到 /index.html

我不能使用接受的答案,因為我沒有 MVC 和 HomeController,更新問題中建議的方式也不起作用,我明白了No HTTP resource was found that matches the request URI 'http://admin.localhost:33333/'. No route providing a controller name was found to match request URI 'http://admin.localhost:33333/'

將您的 Index.html 移動到項目的根目錄。然後install-package Microsoft.Owin.StaticFiles在包管理器控制台中添加以下程式碼:

public class Startup
{
   public void Configuration(IAppBuilder app)
   {

       const string rootFolder = ".";
       var fileSystem=new PhysicalFileSystem(rootFolder);
       var options = new FileServerOptions
                     {
                         EnableDefaultFiles = true,
                         FileSystem = fileSystem
                      };

       app.UseFileServer(options);

   }
}

預設情況下,這將提供您的 Index.html。

您可以查看 Scott Allen 的部落格以獲取更多資訊:

<http://odetocode.com/blogs/scott/archive/2014/02/10/building-a-simple-file-server-with-owin-and-katana.aspx>

引用自:https://stackoverflow.com/questions/28476795