Requirement: A website needs to be split out of another site due to business expansion. The new site and old site have the same content, but the specific column shows different forms. The original website has been running for many years and has a large number of images, which will also be used on the new site. The task is:
Ensure that the image content of the two websites is synchronized, that is, an image is added to the original site, and the image can be used by the new site.
Analysis: assume that the domain names of the two sites are: http://www.a _ image.com/and http://www. B _image.com/
1. Copy the images to the two servers, and upload the images to the two servers at the same time. This method is simple, but occupies a large disk space and is difficult to maintain.
2. directly write http://www.a_image.com/on the page of http://www. B _ image.com. This method is similar to "image leeching", but the workload is huge. You need to change the link for each added image. In the future, the maintenance work will be huge.
3. One server retains images, and the other server accesses images through IIS network sharing. However, open file sharing poses a threat to server security.
analyze the page running principle:
when a browser requests a page from IIS, IIS forwards the request to ASP. net HTTP pipeline, an HTTP pipeline is a group of controlled objects (htppmodel), these objects process requests in order and convert these requests into general HTML text. The HTTP pipeline entry is the entity of the httpruntime class. The httpruntime class entity selects an httpapplication object from the memory Program pool. The main task of httpapplication is to find such a class entity (httphandler) to process requests. The application can redefine the HTTP processing handle ing list in Web. config. The processing handle of the .jpg resource request is defined according to the period in web.config.
the verb list can be a list of HTTP predicates separated by commas (,) (such as" Get, put, post "), the path attribute can also contain a single URL path or a simple wildcard string (for example *. aspx); Type specifies a comma-separated class/assembly combination. ASP. NET first searches for the Assembly DLL in the dedicated \ bin directory of the application, and then searches for the Assembly DLL in the system assembly cache.
in terms of Code , to implement a custom HTTP processing handle, you only need to implement an ihttphandler interface, the methods include isreusable and processrequest. The isreusable method is used to return information about reusable class objects in the application pool. This method can be used for multiple request services at the same time, while the processrequest method accepts an httpcontext object as a parameter, to obtain the access permissions for the request and response.
Solution: For www. when a B _image.com/images/book.jpg request arrives at IIS, Asp. net initializes an image generator class, in which www. a_image.com/images/book.jpg is retrieved and then the image stream is returned to the HTTP response. The process of receiving and processing HTTP requests is completed in the processrequest method. The client does not know what actually happened.
Code:
Using system;
Using system. net;
Using system. IO;
Using system. Web;
Using system. drawing;
Using system. Drawing. imaging;
namespace webhttpmodule
{< br> public class imagelink: ihttphandler
{< br> Public void processrequest (httpcontext context)
{< br> string url = context. request. URL. tostring ();
// If request is http://www. B _ image.com/, then changed to request http://www.a _ image.com/
url = URL. replace ("http://www. B _ image.com/", "http://www.a _ image.com/");
// If the relative path is used, manually add the complete URL path
If (URL. indexof ("http://www.a _ image.com/") <= 0)
{
Url = "http://www.a _ image.com/" + URL;
}
// Create one WebClient object, used to request the image of http://www.a _ image.com/
WebClient client = new WebClient ();
Stream stream = client. openread (URL );
Image image = image. fromstream (Stream );
Context. response. contenttype = "image/JPEG ";
// Write the image into the HTTP Response and return the request to the client
Image. Save (context. response. outputstream, imageformat. JPEG );
Image. Dispose ();
}
Public bool isreusable
{
Get
{
Return true;
}
}
}
}
Configuration:
1. Compile the above Code as imagelink. DLL, create the image directory images in the same path as machine A on server B, create the bin directory under it, and set imagelink. copy the DLL and set the images directory to an application on server B so that the corresponding program ing (Add corresponding extensions) can be set for the application ), to send a JPG suffix request to ASP.. net.
2. Configure the code in Web. config to set httphandlers. Now we need to transfer the JPG file requests under the images directory to imagelink for processing. The final setting is as follows:
<Httphandlers>
<Add verb = "*" Path = "images/*. jpg" type = "webhttpmodule. imagelink, imagelink"/>
</Httphandlers>
Copy web. config to the images directory.
now the big job is done! All JPG images requested by server B are retrieved from server a, but cannot be seen on the client.