【问题标题】:How to serve index.html with web api selfhosted with OWIN如何使用 OWIN 自托管的 web api 提供 index.html
【发布时间】:2015-04-13 03:34:22
【问题描述】:

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

我有一个带有 Web api 的 SPA (AngularJS),它是由 Owin 自托管的。我使用 Nancy 来提供页面,但我想摆脱 Nancy 并使用 Index.html 作为我的单个页面。

我在这里看到了这个问题: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/'

【问题讨论】:

    标签: asp.net-web-api routing single-page-application owin


    【解决方案1】:

    将您的 Index.html 移动到项目的根目录。然后在 Package Manager Console 中 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

    【讨论】:

    • 谢谢,这很有帮助。只是一个建议:包似乎是复数形式:Microsoft.Owin.StaticFiles
    • 我们可以使用这种技术在同一个端口中同时托管 UI(角度应用程序)和 WebApi 吗?如果是,那么您可以分享代码吗?谢谢
    猜你喜欢
    • 2019-02-24
    • 2015-01-09
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多