如何使用OWIN自托管Web API服务index.html文件

32
这是一个简单的问题,但我找不到答案。
我有一个使用AngularJS构建的SPA,配合使用Owin自托管Web api。我目前使用Nancy来提供页面服务,但是我想要摆脱Nancy,使用Index.html作为我的单页应用。
我在这里看到了这个问题:如何将除Web API之外的所有内容路由到/index.html 我不能使用已接受的答案,因为我没有MVC和HomeController,更新后的问题中建议的方法也不起作用,我收到了“未找到与请求URI'http://admin.localhost:33333/'相匹配的HTTP资源”和“未找到提供控制器名称以匹配请求URI'http://admin.localhost:33333/'的路由”这两个错误信息。
1个回答

45

将Index.html文件移动到项目的根目录。然后在包管理器控制台中安装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


谢谢,这非常有帮助。只是一个建议:包似乎是复数形式的:Microsoft.Owin.StaticFiles。 - sp3ctum
1
我们能否使用这种技术在同一端口上托管UI(Angular应用程序)和WebApi?如果可以,您能分享代码吗?谢谢。 - AustinTX

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接