简体   繁体   English

ASP.NET MVC Route:绕过路径的静态文件处理程序

[英]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): 当我点击静态文件(.html,.txt等)时,它们会被StaticFile处理程序而不是我的处理程序拾取,所以我试图像这样关闭StaticFile处理程序(简化):

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

This turns off the StaticFile handler, but MVC still doesn't pick up the route. 这会关闭StaticFile处理程序,但MVC仍然不会选择路由。

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. 我宁愿不再创建自己的处理程序并注入ASP请求堆栈,因为看起来应该有一个MVC快乐的方法来执行此操作。

Any thoughts? 有什么想法吗? And thanks. 谢谢。

There are a couple options here. 这里有几个选项。

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

If you really want all requests running through the asp.net pipe then you need. 如果你真的想要通过asp.net管道运行所有请求,那么你需要。

<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. 考虑到如何在mvc站点上,大多数静态文件是从几个众所周知的目录处理的,这是一个更好的选择。

The best solution for this is probably to use a URL Rewriter to proxy the requests from the original URL to an MVC friendly alternative. 对此最好的解决方案可能是使用URL重写器将来自原始URL的请求代理到MVC友好替代方案。

For example, say you need to be able to automatically load relative URLs in content served from a database. 例如,假设您需要能够自动加载从数据库提供的内容中的相对URL。

RewriteRule ^/load/(\d+)/rel(\?.*)? -   [L]
RewriteRule ^/load/(\d+)/(.*)$  /load/$1/rel?path=$2    [P,QSA,L,NC]

Those rules will let you set up two MVC routes: 这些规则将允许您设置两个MVC路由:

routes.MapRoute(
    "Load Item",
    "load/{itemId}",
    new { controller = "Load", action = "Index" }
    );

routes.MapRoute(
    "Load Relative Item",
    "load/{itemId}/rel",   //?path=
    new { controller = "Load", action = "Relative" }
    );

And then your action methods are straightforward: 然后你的行动方法很简单:

 public ActionResult Index(int itemId) { ... }

 public FileStreamResult Relative(int itemId, string path) { ... }

The first rewrite rule is there to prevent paths containing /rel from being rewritten; 第一个重写规则是防止包含/ rel的路径被重写; they're already in their final form. 他们已经处于最终状态。 The second rewrite rule proxies (instead of redirecting) the request. 第二个重写规则代理(而不是重定向)请求。

eg 例如

/load/1234/file.xml

becomes

/load/1234/rel?path=file.xml

Proxying the request instead of doing a client redirect enables nested relative paths to work as well (and hides the secret sauce from the end user.) 代理请求而不是进行客户端重定向使嵌套的相对路径也可以工作(并隐藏最终用户的秘密酱。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM