-->

Microsoft.Owin.StaticFiles works in console host b

2019-06-16 17:12发布

问题:

I have Microsoft.Owin.FileServer (v2.1.0) set up in my Owin pipeline, and setting up FileServerOptions with EnableDirectoryBrowsing = true works great for showing the directory contents in both my console host and iisexpress.

It's when I try to view a particular file (so, the StaticFiles part) I have problems in iisexpress. Still works great in the console host, but in iisexpress I get a 404:

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Most likely causes:
- The directory or file specified does not exist on the Web server.
- The URL contains a typographical error.
- A custom filter or module, such as URLScan, restricts access to the file.

I do have the latest Microsoft.Owin.Host.SystemWeb referenced in the web host.

回答1:

Adding <modules runAllManagedModulesForAllRequests="true"> didn't work for me (VS2013, IIS Express).

Forcing all requests to use the Owin pipeline did:

(in web.config)

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


回答2:

I had to add the following setting:

<modules runAllManagedModulesForAllRequests="true">

to get the module that Microsoft.Owin.Host.SystemWeb automatically registers to run for routes like *.txt, *.js that IIS was assuming were static files to run through the Owin pipeline.

This setting does have performance implications for actual static files, but this works for me.



回答3:

I've just struggled with this for the last couple of hours, adding the handler below did work however I don't believe this was the correct approach, it caused public void Configuration(IAppBuilder appBuilder) to be invoked twice.

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

I did some reading and found https://docs.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-middleware-in-the-iis-integrated-pipeline which then lead me to use UseStageMarked().

So now my call to UseStaticFiles() is followed by a called to UseStageMarker() like so:

appBuilder.UseStaticFiles();
//allows owin middlwares to be executed earlier on in the pipeline.
appBuilder.UseStageMarker(PipelineStage.Authenticate);

There is a very good read on it here:

You can find UseStageMarker inside the Microsoft.Owin package here: https://www.nuget.org/packages/Microsoft.Owin/

I hope this helps someone else.

Thanks

Steve