ASP.NET MVC 路由:绕过路径的静态文件处理程序 [英] ASP.NET MVC Route: bypass staticfile handler for path

查看:18
本文介绍了ASP.NET MVC 路由:绕过路径的静态文件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用谷歌搜索和修补了几个小时,但并没有取得太大进展,所以希望这里有人可以提供帮助.

我正在尝试将所有请求发送到某个路径,由 3rd 方组件处理.

编辑而且我需要对所有其他路径的所有请求才能正常运行.

我正在使用带有通配符映射的路由处理程序,如下所示:

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

所有传统路由都正确转发到处理程序,它很好地转发到第 3 方组件.当我点击静态文件(.html、.txt 等)时,它们会被 StaticFile 处理程序而不是我的处理程序接收,所以我试图像这样(简化)关闭 StaticFile 处理程序:

<处理程序><remove name="StaticFile"/></处理程序></system.webServer>

这会关闭 StaticFile 处理程序,但 MVC 仍然没有选择路由.

我不想退回到创建自己的处理程序并将其注入 ASP 请求堆栈,因为似乎应该有一种 MVC-happy 的方式来做到这一点.

有什么想法吗?谢谢.

解决方案

这里有几个选项.

http://www.hanselman.com/blog/BackToBasicsDynamicImageGenerationASPNETControllersRoutingIHttpHandlersAndRunAllManageds.

如果您真的希望所有请求都通过 asp.net 管道运行,那么您需要.

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

更新

另一种选择,特别是如果您需要绕过静态处理程序仅限于您网站的一个子集,是使用以下内容

 <add name="ApiURIs-ISAPI-Integrated-4.0"路径="/子目录/*"动词="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"type="System.Web.Handlers.TransferRequestHandler"preCondition="integratedMode,runtimeVersionv4.0"/>

考虑到在 mvc 站点上,大多数静态文件是从几个众所周知的目录处理的,这是一个更好的选择.

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.

解决方案

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.

这篇关于ASP.NET MVC 路由:绕过路径的静态文件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆