【问题标题】:How to set default static web page for Katana/Owin self hosted app?如何为 Katana/Owin 自托管应用程序设置默认静态网页?
【发布时间】:2014-10-18 03:35:40
【问题描述】:

我使用 Owin 自托管控制台应用程序建立了一个网站。我提供静态文件没有问题,网站静态部分的“根”工作正常,Web API 路由也工作正常。

如果我浏览到:

http://localhost/index.html

它呈现出我所期望的一切。但我还没有弄清楚如何设置它以便浏览到:

http://localhost

呈现 index.html(作为默认视图)。这只是在 IIS 样式的站点下工作。如何使其与 Owin 自助主机一起使用?

【问题讨论】:

  • 你能给我们看看你的 Startup.cs 文件吗?

标签: asp.net owin katana self-hosting


【解决方案1】:

就我而言,我错过了

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

在我的 web.config 的 system.webServer 部分。

【讨论】:

    【解决方案2】:

    也许这是一个迟到的答案,但如果您只需要一个默认文档,那么可以使用更少的代码:

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

    无论出于何种原因,它都应该在builder.UseStaticFiles 之前调用。

    Microsoft.Owin.StaticFiles 的版本是 3.0.1

    【讨论】:

    • 你救了我的命,在UseStaticFiles 之前致电UseDefaultFiles 是关键!
    【解决方案3】:

    也许这个 late 答案可以帮助任何其他人:) 我刚刚在使用 SelfHost Owin 应用时遇到了同样的问题。

    我找到的解决方案是从 IFileSystem 接口实现一个类,该接口封装了一个 PhysicalFileSystem 类(它也从 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");
    };
    

    【讨论】:

    • 这非常有用,我实现了一个类似的类来服务器 index.html 所以我的水疗工作正常,我希望我能给你更多的支持
    【解决方案4】:

    更详细的 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- 上一个代码工作的另一个步骤:确保将 Web 目录(以及所有嵌套目录)中所有文件的 Copy to output directory 属性设置为 Copy Always [选择文件 |按 F4,或右键单击属性 |去Copy to output directory]

    就是这样:)

    【讨论】:

      【解决方案5】:

      我是这样做的:

      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);
      

      【讨论】:

      • 自写这篇文章以来,这种情况发生了变化吗?我确实有这个,虽然我可以去http://localhost:5000/index.html查看页面,但如果我只是去http://localhost:5000,我只会得到404。
      • @Eddie 这确实适用于所有最新版本的 NuGet 包。我认为您只需要为 index.html 添加正确的相对文件路径。我的是“app/templates/index.html”,当我在 Chrome 中输入localhost:8080 时,这有效:)
      • @Eddie 通过将其“复制到输出目录”属性设置为“始终复制”或“如果较新则复制”来确保 html 文件位于构建位置。如果找不到文件,您没有得到异常,这是一种耻辱!!!
      • @JDTLH9 啊相对路径!我只是在写“index.html”,以为它会找到它。干杯
      • 如果根不在当前路径的正下方,这似乎不起作用。例如,如果根路径是 ..\\web 它只能提供默认文档,但所有其他引用(.js、.css 等)都会返回 404,因为它会尝试在定义appBuilder...奇怪的行为...
      猜你喜欢
      • 2015-01-09
      • 2018-03-16
      • 2018-05-25
      • 2015-06-30
      • 2012-10-27
      • 2018-06-28
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多