前一篇中我们已经了解了Action Filter 与 内置的Filter实现,现在我们就来写一个实例。就写一个防盗链的Filter吧。
首先继承自FilterAttribute类同时实现IActionFilter接口,代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 防盗链Filter.
/// </summary>
public class AntiOutSiteLinkAttribute : ActionFilterAttribute, IActionFilter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public AntiOutSiteLinkAttribute(FileType fileType)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.FileType = fileType;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 请求的文件类型.(文件或图片)
/// </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public FileType FileType
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IActionFilter 成员#region IActionFilter 成员
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContextBase httpContext = filterContext.HttpContext;
if (null != httpContext.Request.UrlReferrer)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string serverDomain = httpContext.Request.Url.Host;
string refDomain = httpContext.Request.UrlReferrer.Host;
if (GetRootDomain(refDomain).Equals(GetRootDomain(serverDomain), StringComparison.OrdinalIgnoreCase))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;//如果根域名相同就返回
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ContentResult cr = new ContentResult();
if (FileType == FileType.Image)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cr.ContentType = "image/jpeg";
FileInfo fi = new FileInfo(httpContext.Server.MapPath("~/Content/images/outsitelink.jpg"));
if (fi.Exists)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
httpContext.Response.WriteFile(fi.FullName);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Bitmap bmp = new Bitmap(200, 50);
Graphics g = Graphics.FromImage(bmp);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
g.FillRectangle(Brushes.White, 0, 0, 200, 50);
g.DrawString("请不要盗链", new Font("Arial", 15), Brushes.Red, new PointF(0, 0));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
bmp.Save(httpContext.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cr.ContentType = "text/html";
cr.Content = string.Format("请不要盗链。返回<a href='{0}'>{1}</a>", Utils.AbsoluteWebRoot, BlogSettings.Instance.Name);
}
//将当前的上下文的ActionResult设置为我们的cr(ContentResult)
filterContext.Result = cr;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取网站的根域名
/// </summary>
/// <param name="domain">网站的域名,不带"Http://"</param>
/// <returns></returns>
private string GetRootDomain(string domain)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (string.IsNullOrEmpty(domain))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("参数'domain'不能为空");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] arr = domain.Split(new[]
{ '.' }, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length <= 2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return domain;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return arr[arr.Length - 2] + "." + arr[arr.Length - 1];
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public enum FileType
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
File = 1,
Image
}
然后我们建立一个用于处理文件请求的Controller,并应用上我们刚才建立的Filter:
public class FilesController : BaseController
{
[AntiOutSiteLink(FileType.Image)]
public ActionResult Image(string file)
{
return Content("Image From 4mvc");
}
[AntiOutSiteLink(FileType.File)]
public ActionResult File(string file)
{
return Content("File From 4mvc");
}
}
简单测试一下:
对于ActionFilter的应用是很广泛的,这需要你的灵活运用。对于其他应用,你可以参考我之前翻译的ASP.NET MVC Action Filter - 缓存与压缩 这一篇文章。
Enjoy!这个没有什么代码,就暂不传代码了。
power by [lulu] 原文地址