Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

c# - ASP.NET MVC Route: bypass staticfile handler for path

I've been googling and tinkering for a couple hours and haven't really made much progress, so hopefully someone here can help.

I'm trying to get all requests to a certain path to be handled by a 3rd party component.

EDIT And I need all requests to all other paths to behave normally.

I'm using a route handler with a wildcard mapping like this:

routes.Add(new Route("pathiwant/{*EverythingElse}", new MyRouteHandler()));

All traditional routes forward correctly to the handler, which forwards nicely to the 3rd party component. When I hit static files (.html, .txt, etc.), they get picked up by the StaticFile handler instead of my handler, so I'm attempting to turn off the StaticFile handler like so (simplified):

<system.webServer>
  <handlers>
    <remove name="StaticFile"/>
  </handlers>
</system.webServer>

This turns off the StaticFile handler, but MVC still doesn't pick up the route.

I'd prefer not to fall back on creating my own handler and injecting into the ASP request stack since it seems like there should be an MVC-happy way to do this.

Any thoughts? And thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a couple options here.

http://www.hanselman.com/blog/BackToBasicsDynamicImageGenerationASPNETControllersRoutingIHttpHandlersAndRunAllManagedModulesForAllRequests.aspx

If you really want all requests running through the asp.net pipe then you need.

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

Update

Another option, especially if your need to bypass the static handler is constrained to a subset of your site, is to use the following

  <add name="ApiURIs-ISAPI-Integrated-4.0"
     path="/subdirectory/*"
     verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
     type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />

Considering how on mvc sites, most static files are handled from a couple well known directories, this is a better option.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...