如何使用通过OWIN自托管的Web API来提供index.html [英] How to serve index.html with web api selfhosted with OWIN

查看:207
本文介绍了如何使用通过OWIN自托管的Web API来提供index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该是一个简单的问题,只是找不到答案.

Should be an easy question, just can't find the answer.

我有一个带有Web API的SPA(AngularJS),该API由Owin自托管.我使用Nancy服务该页面,但我想摆脱Nancy并使用Index.html作为我的单个页面.

I have an SPA (AngularJS) with web api which is self hosted with Owin. I use Nancy to serve the page, but I would like to get rid of Nancy and use Index.html as my single page.

我在这里看到了这个问题:如何将除Web API之外的所有路由都路由到/index.html

I've seen this question here: How to route EVERYTHING other than Web API to /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/'

I can't use accepted answer as I don't have MVC and HomeController, the way suggested in the updated question doesn't work either, I get 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移至项目的根目录.然后在Package Manager控制台中install-package Microsoft.Owin.StaticFiles并添加以下代码:

Move your Index.html to the root of your project. Then install-package Microsoft.Owin.StaticFiles in Package Manager Console and add the code below:

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提供服务.

This will serve up your Index.html by default.

您可以查看Scott Allen的博客以获取更多信息:

You can checkout Scott Allen's blog for more reading:

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

这篇关于如何使用通过OWIN自托管的Web API来提供index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆