在特定路由前缀配置OWIN静态文件服务器

Geo*_*uer 8 asp.net fileserver owin

我正在尝试将我的内容保存在非默认位置(例如in 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
Run Code Online (Sandbox Code Playgroud)

这就是我在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)
 );
Run Code Online (Sandbox Code Playgroud)

根据我的理解和以前的实验,这非常简单 - 当请求开始时/__underscore 使用简单的静态文件服务器.然而,当我走到/__underscore我得到404错误.

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

我错过了什么?

hai*_*770 14

您还需要指定RequestPath:

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

根据你的评论:

如果您无法下载文件时,尽量明确地登记OwinHttpHandlerWeb.Config:

<system.webServer> 
    <handlers> 
        <add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/> 
    </handlers> 
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

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

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="FormsAuthenticationModule" />
    </modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)