Owin UseStaticFiles not respecting RequestPath

2019-06-16 11:23发布

问题:

**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!



标签: owin