【问题标题】:Configure an OWIN static file server at a specific route prefix在特定路由前缀处配置 OWIN 静态文件服务器
【发布时间】:2014-07-28 21:08:31
【问题描述】:

我正在尝试将我的内容保存在非默认位置(例如bower_components/packages/../tools)。作为实验的一部分,我正在尝试设置一个 asp.net mvc 5 应用程序,在该应用程序中,点击某个路由可以让我浏览 undersorejs 包目录中的文件。

我有以下 nuget 包(除了默认的)

Install-Package underscore.js
Install-Package Microsoft.Owin.StaticFiles
Install-Package Microsoft.Owin.Host.SystemWeb

这就是我在 OWIN 启动课程中的内容

 var fileSystem = new PhysicalFileSystem(
                    HttpContext.Current.Server.MapPath("~")+"/../packages/underscore.js.1.6.0"
                  );
 var options = new FileServerOptions {EnableDirectoryBrowsing = true, FileSystem = fileSystem};

 app.MapWhen(ctx => 
      ctx.Request.Uri.AbsolutePath.StartsWith("/__underscore"), 
      ab => ab.UseFileServer(options)
 );

根据我的理解和之前的实验,这非常简单 - 当请求以 /__underscore use the simple static file server 开头时。但是,当我转到 /__underscore 时,我收到 404 错误。

但是,放置断点我可以看到 UseFileServer lambda 在启动时执行一次,然后不再执行,而谓词 lambda 在每个请求上都被调用(并返回正确的值)。

我错过了什么?

【问题讨论】:

  • 应用程序是如何托管的?
  • @haim770 iis express。目前只是默认的 MVC asp.net web 项目和 f5

标签: asp.net owin fileserver


【解决方案1】:

您还需要指定RequestPath

var options = new FileServerOptions {
                      EnableDirectoryBrowsing = true,
                      FileSystem = fileSystem,
                      RequestPath = PathString.FromUriComponent("/__underscore")
                      };

根据您的评论:

如果您无法下载文件,请尝试在您的Web.Config 中明确注册OwinHttpHandler

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

或者,您可以将runAllManagedModulesForAllRequests 设置为'true':

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="FormsAuthenticationModule" />
    </modules>
</system.webServer>

【讨论】:

  • 啊哈!所以次要问题 - 为什么处理程序按该顺序运行?每当谓词返回 true 时,第二个 lambda 不应该运行吗?
  • 有趣,它现在允许导航,但当我尝试下载文件时会导致 404
  • 文件扩展名是什么?
  • 太好了,成功了!任何洞察为什么该处理程序是必要的?我相信 Owin 很重要,我真的很想尽可能深入地了解这方面的情况。
  • 显然 IIS StaticFileHandler 正在接管此类请求,并且当没有显式配置运行其他模块(Owin)时,它(正确地)以 404 中止请求,因为没有静态文件匹配 __underscore/..... .
猜你喜欢
  • 2017-07-12
  • 1970-01-01
  • 2012-01-02
  • 2010-10-25
  • 1970-01-01
  • 2015-12-18
  • 2023-02-09
  • 1970-01-01
  • 2015-06-06
相关资源
最近更新 更多