简体   繁体   English

将所有请求的URL重写为ASP.NET 3.5中的文件夹-IIS 7

[英]Rewrite URL for all requests to a folder in ASP.NET 3.5 - IIS 7

We have a number of existing clients that point to urls like: 我们有许多现有的客户指向以下网址:

http://sub1.site.com/images/image1.jpg http://sub1.site.com/images/image1.jpg

/images is a virutal directory that points to a directory that actually contains image1.jpg on that server. / images是虚拟目录,指向该服务器上实际包含image1.jpg的目录。

We're moving all of the files out of this directory and onto a separate server that will not run this same application. 我们会将所有文件移出该目录,并移到不会运行同一应用程序的单独服务器上。

The file will now only be available at: 该文件现在仅在以下位置可用:

http://sub2.site.com/image1.jpg http://sub2.site.com/image1.jpg

What is the best way to make it so clients requesting http://sub1.site.com/images/image1.jpg will get the content that now resides at http://sub2.site.com/image1.jpg ? 做到这一点的最佳方法是什么,以便要求http://sub1.site.com/images/image1.jpg的客户获得现在位于http://sub2.site.com/image1.jpg的内容?

A few requirements: 一些要求:

  • We need the actual content to be returned through that url - not a 302 response. 我们需要通过该网址返回实际内容-而不是302响应。
  • We cannot modify the IIS server configuration - only the web.config for the site 我们无法修改IIS服务器配置-只能修改网站的web.config
  • Again, we're running asp.net 3.5 同样,我们正在运行asp.net 3.5

Thanks. 谢谢。

Not totally sure this would work, but you could setup URL Routing on the old site so all requests are sent to a handler and within that handler you could do a web request to get the file from it's new location. 不能完全确定这是否可行,但是您可以在旧站点上设置URL路由,以便将所有请求发送到处理程序,并且可以在该处理程序中执行Web请求以从新位置获取文件。

I use a variation of the process to map image URLs to different locations and my handler does some database queries to get the mapped relationship and provide the correct image. 我使用该过程的一种变体将图像URL映射到不同的位置,并且我的处理程序执行一些数据库查询以获取映射的关系并提供正确的图像。 I don't see why you could do a web request to get the image. 我不明白为什么您可以提出网络请求以获取图像。

Since you are using IIS7, you can use the built in URL rewrite module . 由于您使用的是IIS7,因此可以使用内置的URL重写模块

You would want an inbound and an outbound rule to change \\images\\image1.jpg to \\image1.jpg 您可能希望使用入站和出站规则将\\ images \\ image1.jpg更改为\\ image1.jpg

It can get pretty involved, but this should be rather simple. 它可以涉及很多,但这应该很简单。

Assuming you can add handlers to your site (as in add a DLL to your /bin directory in the site) and with the restriction that you can't send 302 responses for better performance, then alternatively you could write a custom handler to grab all requests that match that URL pattern, do the web request for the sub2.site image from the original site via web client code, then serve it back out of the original site, sub1.site.com. 假设您可以将处理程序添加到站点(例如在站点的/ bin目录中添加DLL),并且由于无法发送302响应而限制了性能,那么您可以编写一个自定义处理程序以获取所有请求与该URL模式匹配的请求,然后通过Web客户端代码从原始网站对sub2.site图像进行网络请求,然后将其从原始网站sub1.site.com中提供回来。

See How To Create an ASP.NET HTTP Handler by Using Visual C# .NET for the very basics of creating and setting up a custom handler. 有关创建和设置自定义处理程序的基本知识,请参见如何使用Visual C#.NET创建ASP.NET HTTP处理程序。 Then use the HttpWebRequest to make the request of sub2.site.com, as in the guide A Deeper Look at Performing HTTP Requests in an ASP.NET Page . 然后,使用HttpWebRequest发出sub2.site.com的请求,如指南“深入了解ASP.NET页面中的执行HTTP请求”中所述 Plus a little other code to handle errors, timeouts, passing the image through with as little processing and memory usage as possible, etc. 加上一些其他代码来处理错误,超时,以尽可能少的处理和内存使用率传递图像等。

Depending on the response time/lag between the two servers, this may be slow, but it would fit all your requirements. 根据两台服务器之间的响应时间/延迟,这可能会很慢,但它可以满足您的所有要求。 But if the point of moving the images to site 2 was for performance (CPU or memory) or bandwidth limitations, then this solution would nullify any gains — and would actually make things worse. 但是,如果将映像移至站点2的目的是出于性能(CPU或内存)或带宽限制的考虑,那么该解决方案将抵消任何收益-实际上会使情况变得更糟。 But if they were moved for other business or technical reasons though, then this solution might be helpful still. 但是,如果由于其他业务或技术原因而搬迁,则此解决方案可能仍然有用。

If you have other control over the server or anything upstream from the server, you could use mod_proxy (or similar Windows/IIS tool) to intercept those URLs and forward them to another server and respond back with the real request. 如果您对服务器或服务器上游的任何内容拥有其他控制权,则可以使用mod_proxy(或类似的Windows / IIS工具)来拦截这些URL,并将其转发到另一台服务器,然后以实际请求进行响应。 Depending on your network configuration and available servers, this could be the simplest, best performing solution. 根据您的网络配置和可用服务器,这可能是最简单,性能最好的解决方案。

Can IIS be configure to forward request to another web server? 可以将IIS配置为将请求转发到另一个Web服务器吗? on serverfault has a quick process and link for an IIS 7.5 solution. serverfault上的IIS 7.5解决方案具有快速的过程和链接。

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

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