Create Customhandler.jpghandler
Public classjpghandler:ihttphandler{ Public voidProcessRequest (HttpContext context) {Try { //get the file server-side physical path stringFileName =context. Server.MapPath (context. Request.filepath); //if Urlreferrer is empty, a picture of the default forbidden hotlinking is displayed if(Context. Request.UrlReferrer.Host = =NULL) {context. Response.ContentType="Image/jpeg"; Context. Response.WriteFile ("/error.jpg"); } Else { //If the urlreferrer does not contain its own site host domain name, a default forbidden hotlinking picture is displayed if(Context. Request.UrlReferrer.Host.Contains ("localhost")) {context. Response.ContentType="application/pdf"; Context. Response.WriteFile (FileName); } Else{context. Response.ContentType="Image/jpeg"; Context. Response.WriteFile ("/error.jpg"); } } } Catch(Exception ex) {context. Response.ContentType="Image/jpeg"; Context. Response.WriteFile ("/error.jpg"); } } Public BOOLisreusable {Get{return true; } }}
Reference the compiled DLL file in the project and register the handler in the project
<system.webServer> "Myjpghandler" path= " *.jpg " verb="*" type="Customhandler.jpghandler, Customhandler " />
MVC creates and registers an anti-theft chain