Owin UseStaticFiles not respecting RequestPath

孤者浪人 提交于 2019-12-10 12:52:09

问题


**EDIT: NOTE: This appears to be fixed in later versions of the Owin StaticFiles middleware, so if you have this probem, simply upgrade **

My OWIN configuration has this:-

   string root = AppDomain.CurrentDomain.BaseDirectory;
   var staticFilesOptions = new StaticFileOptions();
   staticFilesOptions.RequestPath = new PathString("/foo");
   staticFilesOptions.FileSystem = new PhysicalFileSystem(Path.Combine(root, "web"));
   app.UseStaticFiles(staticFilesOptions);

When I hit /foo/app/app.js I get a 404 error, when I hit /web/app/app.js the file is returned.

How is RequestPath meant to work in conjunction with PhysicalFileSystem?


回答1:


I tested this code snippet and it works for me from this sample project.

app.UseFileServer(new FileServerOptions()
{
    RequestPath = new PathString("/foo"),
    FileSystem = new PhysicalFileSystem(@".\web"),
});

Make sure you have this in your web.config:

<system.webServer>
    ...
    <modules runAllManagedModulesForAllRequests="true"></modules>
    ...
</system.webServer>



回答2:


RequestPath you define a virtual path for your static resources used in requests, see this as a route to your static files.

Using PhysicalFileSystem you define the path for the static file lookup.

So, if there is a request for what you've defined in RequestPath the path-part is translated to the directory on your harddisk.

In your case you should receive the status code of 404 for web/app.js (as this is your directory and not your route) and a 200 for foo/app.js.




回答3:


I got the same problem. Acording to the answer of @Rafael here. Add the following configuration to Web.config then it resolves my issue:

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

Hope this help the other people!



来源:https://stackoverflow.com/questions/22387555/owin-usestaticfiles-not-respecting-requestpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!