【问题标题】:How to use an absolute directory path for static files with OWIN self-hosting ASP app?如何使用 OWIN 自托管 ASP 应用程序为静态文件使用绝对目录路径?
【发布时间】:2018-05-25 01:51:42
【问题描述】:

我有一个带有该选项的 OWIN 自托管应用程序,并且我一直在通过 appBuilder.UseStaticFiles("/Public/...") 选项将请求传递给静态文件。

但这会将“构建”目录作为根目录。如何将目录指定为绝对路径?

【问题讨论】:

    标签: asp.net owin static-files


    【解决方案1】:

    我在这里找到了解决方案:https://richardniemand.wordpress.com/2015/06/18/hosting-web-api-and-static-content-with-owin/

    然后根据博文稍作修改,我需要的代码:

    PhysicalFileSystem physicalFileSystem = new PhysicalFileSystem(@"C:/www/websiteName");
    FileServerOptions options = new FileServerOptions
    {
        RequestPath = new Microsoft.Owin.PathString("/Public"),
        EnableDirectoryBrowsing = true,
        EnableDefaultFiles = true,
        FileSystem = physicalFileSystem
    };
    options.StaticFileOptions.FileSystem = physicalFileSystem;
    options.StaticFileOptions.ServeUnknownFileTypes = true;
    options.DefaultFilesOptions.DefaultFileNames = new[]
    {
        "index.html"
    };
    appBuilder.UseFileServer(options);
    

    然后在我的 AJAX 请求中我可以使用 URI /Public/.../something.js

    【讨论】:

      【解决方案2】:

      OP 的回答使我的绝对文件夹路径可用于目录浏览,但文件仍然抛出 404。web.config 中的这些行使一切按预期工作

      <system.webServer>
          <handlers>
            <add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/>
          </handlers>
        </system.webServer>
      </configuration>
      

      上面的配置也使我的本地静态文件无法访问。 所以我不得不调用配置方法两次。

      app.UseFileServer(options);
      app.UseFileServer();
      

      【讨论】:

        猜你喜欢
        • 2022-10-19
        • 2014-10-18
        • 2020-12-11
        • 2021-10-09
        • 1970-01-01
        • 1970-01-01
        • 2011-09-18
        • 1970-01-01
        • 2018-04-11
        相关资源
        最近更新 更多