In Asp.net development, if you want to display the default image when the image cannot be found, we have the following methods:
1. JS
You can obtain the object of each image tag on the page. Then set its src attribute to OK.
Code<SCRIPT>
Function window. onload () {var IMG = document. getelementbyid ("form1 "). getelementsbytagname ("IMG") for (VAR I = 0; I
</Scirpt>
2. webconfig Configuration
Configuring webconfig can easily and practically solve some difficult problems. First, the webconfig configuration file must be loaded for each solution running. We can write a public class to inherit ihttphandler. Configure
(1). Class Method
Code public imagemanager () {// todo: add the constructor logic here //} private const string defaultimage_url = "Default. jpg ";
Public bool isreusable {get {return false ;}}
Public void processrequest (httpcontext context) {// obtain the different physical address of each image in the request string imgphysicalpath = context. Request. physicalpath; system. Drawing. Image image = NULL;
If (file. exists (imgphysicalpath) {// null
} Else {// if the image does not exist, place the default image = system. drawing. image. fromfile (context. server. mappath (defaultimage_url);} // sets the output type context. response. contenttype = "image/JPEG"; // Save the image to the output stream. save (context. response. outputstream, system. drawing. imaging. imageformat. JPEG); image. dispose ();
}
(2). webconfig Configuration
<Add verb = "*" Path = "path/*. jpg" type = "public class name"/>