使用httpHandle来实现,对图片文件的请求做专门的处理 第一步:创建一个类,继承自IHttpHandler,代码如下 C# code using System; using System.Web; namespace CustomHandler{ public class JpgHandler : IHttpHandler{ public void ProcessRequest(HttpContext context){ // 获取文件服务器端物理路径 string FileName = context.Server.MapPath(context.Request.FilePath); // 如果UrlReferrer为空,则显示一张默认的禁止盗链的图片 if (context.Request.UrlReferrer.Host == null){ context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/error.jpg"); }else{ // 如果 UrlReferrer中不包含自己站点主机域名,则显示一张默认的禁止盗链的图片 if (context.Request.UrlReferrer.Host.IndexOf("yourdomain.com") > 0){ context.Response.ContentType = "image/JPEG"; context.Response.WriteFile(FileName); }else{ context.Response.ContentType = "image/JPEG"; context.Response.WriteFile("/error.jpg"); } } } public bool IsReusable{ get{ return true; } } } } 第二步:编译成DLL csc /t:library CustomHandler.cs 第三步:添加编译好的DLL引用到当前站点的bin文件夹下 第四步:在Web.Config 中注册这个Handler C# code <system.web> <httpHandlers> <add path="*.jpg,*.jpeg,*.gif,*.png,*.bmp" verb="*" type="CustomHandler.JpgHandler,CustomHandler" /> </httpHandlers> </system.web> //verb指的是请求此文件的方式,可以是post或get,用*代表所有访问方式。CustomHandler.JpgHandler表示命名空间和类名,CustomHandler表示程序集名。 from:http://www.cnblogs.com/ghfsusan/archive/2011/02/25/1964579.html
View Details利用Global.asax的Application_BeginRequest 实现url 重写 无后缀 <%@ Application Language="C#" %> <script RunAt="server"> void Application_BeginRequest(object sender, EventArgs e) { string oldUrl = System.Web.HttpContext.Current.Request.RawUrl; //获取初始url //~/123.aspx → ~/Index.aspx?id=123 Regex reg = new Regex(@"^\/\d+\.html"); if (reg.IsMatch(oldUrl)) { string id = reg.Match(oldUrl).ToString().Substring(1, reg.Match(oldUrl).ToString().LastIndexOf(".") – 1); Context.RewritePath("~/Index.aspx?id=" + id); } //~/123 → ~/Index.aspx?id=123 Regex reg1 = new Regex(@"^\/\d+$"); if (reg1.IsMatch(oldUrl)) { string id = reg1.Match(oldUrl).ToString().Substring(1); Context.RewritePath("~/Index.aspx?id=" + id); } //~/index/123 → ~/Index.aspx?id=123 Regex reg3 = new Regex(@"^\/index\/\d+$"); if (reg3.IsMatch(oldUrl)) { string id = reg3.Match(oldUrl).ToString().Substring(7); Context.RewritePath("~/Index.aspx?id=" + id); } } </script> from:http://www.jb51.net/article/40587.htm
View Details