Through this article, we will talk about the aspx extension of ASP. NET pages, mainly for the expansion of website business and the synchronization of the image content of the two websites. I hope this article will help you.
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 image to twoServerTo upload images to 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:
LJ@0_.k + S0 when the browser requests a page to IIS, IIS will forward 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 Object selects an httpapplication object from the memory program pool. The main task of httpapplication is to find such a class Object (httphandler) so that it can process requests. The application canWeb. Config to redefine the HTTP processing handle ing list. The period in the following web.configdefines the processing handle of the .jpg resource request:
<Httphandlers> <add verb = "*" Path = "*. jpg" type = "handlertest"/>
The verb list can be a comma-separated HTTP predicate list (such as "Get, put, post") or a wildcard "*"; the path attribute can 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. Itpub personal space; ks ar & v9j
From the code perspective, to implement a custom HTTP processing handle, you only need to implement an ihttphandler interface, including the isreusable and processrequest methods. 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 {public class imagelink: ihttphandler {public void processrequest (httpcontext context)
{String url =
Context. Request. url. tostring (); // If request is http://www. B _ image.com/, then changed to request http://www.a _ image.com/
Urlurl = URL. replace ("http://www. B _ image.com/", "http://www.a _ image.com/"); // 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 to an HTTP Response and return it to the client for an 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>
<AddVerb = "*"
Path = "images/*. jpg" type = "webhttpmodule. imagelink, imagelink"/>
</Httphandlers>
Copy web. config to the images directory.