asp.net picture anti-theft chain Realization Principle Analysis _ practical skill

Source: Internet
Author: User
So let me introduce a way to put hotlinking in the picture.
First, add a httphandlers request, webconfig part of the configuration section as follows:
<add verb= "*" path= "*.jpg" type= "Myhandler,app_code"/>
Then add a class, named MyHandler inherited from IHttpHandler
The class prototype is as follows:
Copy Code code as follows:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
public class Myhandler:ihttphandler
{
public void ProcessRequest (HttpContext context)
{
String FileName = context. Server.MapPath (context. Request.filepath);
if (context. Request.UrlReferrer.Host = null)
{
Context. Response.ContentType = "Image/jpeg";
Context. Response.WriteFile ("~/images/no.png");/replaced picture
}
Else
{
if (context. Request.UrlReferrer.Host.IndexOf ("localhost") >-1)//This is your domain name.
{
Context. Response.ContentType = "Image/jpeg";
Context. Response.WriteFile (FileName);
}
Else
{
Context. Response.ContentType = "Image/jpeg";
Context. Response.WriteFile ("~/images/no.png");
}
}
}
public bool IsReusable
{
get {return true;}
}
Public MyHandler ()
{
}
}

MyHandler inherits from IHttpHandler realizes the URL source to judge whether the JPG picture is hotlinking, in this class takes localhost as an example, modifies the localhost and the images/no.png parameter can
The secondary method can also be applied to URL pseudo static.
Next time chat The picture and the ASP.net program's server separates, welcome everybody to visit this website.

Here is the supplementary
The so-called URL static (statement: Here is said to be pseudo static, the real Web page static will not say can use the simplest request to return response implementation, but as if the amount of data over time will be very large, so do not say) is the Dynamic Web page looks like static. such as: aspnet.aspx?id=5 static: Aspnet-5.html or/ASPNET/5

SEO (Search engine Optimization), more in favor of static web pages, so the static web page for professional seoer or must be mastered, I tell you a way to achieve in the asp.net below the Web page static.

Below give a demo, download address: http://www.jb51.net/codes/23902.html


This demo implements the URL of the Web page pseudo static, the principle of matching the URL of the regular, to carry out the Web page redirection, this demo has a config,<add virtualurl= "~/microsoft*.*" Destinationurl = "~/default.aspx"/>
That is, the request microsoft.html,microsoft.shtml,microsoft.do will be mapped to Default.aspx

This is to redirect the URL to a page, without parameters, I first to introduce the next with the parameters how to implement, I added this rule
<add virtualurl= "~/default-([0-9]*)" destinationurl= "~/default.aspx?id=$1"/>
This regular match, such as the URL (assuming http://www.xx.com/is your domain name), www.xx.com/microsoft-123 map to Www.xx.com/default.aspx?id=123
Then the transfer of parameters is realized.

The transfer of multiple parameters can be implemented with the following, you master want to use what kind of static rules on their own writing is how to achieve it, I give a directory of the URL pseudo static rules
<add virtualurl= "~/default/([a-za-z0-9]*)/([a-za-z0-9]*)" Destinationurl= "~/default.aspx?par1=$1&par2=$2" /> here to pay attention to is the & (&)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.