9

我正在尝试将我的内容保存在非默认位置(例如 inbower_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 使用简单的静态文件服务器。但是,当我前往时,/__underscore我收到 404 错误。

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

我错过了什么?

4

1 回答 1

15

您还需要指定RequestPath

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

根据您的评论:

如果您无法下载文件,请尝试OwinHttpHandler在您的Web.Config:

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

或者,您可以设置runAllManagedModulesForAllRequests为“真”:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="FormsAuthenticationModule" />
    </modules>
</system.webServer>
于 2014-06-08T06:50:14.333 回答