Original address: http://www.cnblogs.com/QLeelulu/archive/2008/10/13/1310419.html
In the previous article we've learned about action filter and the built-in filter implementation, and now we're going to write an instance. Just write a filter for the anti-theft chain.
First inherits from the FilterAttribute class implements the Iactionfilter interface simultaneously, the code is as follows:
// <summary>
/// Anti-theft chain filter.
// </summary>
Public classAntioutsitelinkattribute:actionfilterattribute, Iactionfilter
{
PublicAntioutsitelinkattribute (FileType FileType)
{
this. FileType = FileType;
}
/// <summary>
///requested file type. file or picture)
// </summary>
PublicFileType FileType { get; Set ; }
Iactionfilter Members
/// <summary>
///get the root domain name of the site
/// </summary>
/// <param name= "domain" >The domain name of the website, without "http //"</param>
/// <returns></returns>
Private stringGetrootdomain (stringdomain)
{
if (string. IsNullOrEmpty (domain))
{
throw new ArgumentNullException (" parameter ' domain ' cannot be null " );
}
string[] Arr=domain. Split (New[] { '. ' }, stringsplitoptions.removeemptyentries);
if(arr.) Length<= 2)
{
return domain;
}
Else
{
return arr[arr. length - &NBSP; 2 ]&NBSP; + &NBSP; " . " &NBSP; + arr[arr. length - &NBSP; 1 ";
}
}
}
Public enumFileType
{
File = 1,
Image
}
We then set up a controller to process the file request and apply the filter that we just created:
Public classFilescontroller:basecontroller
{
[Antioutsitelink (Filetype.image)]
PublicActionResult Image (stringfile)
{
returnContent ("Image from 4mvc");
}
[Antioutsitelink (Filetype.file)]
PublicActionResult File (stringfile)
{
returnContent ("File from 4mvc");
}
}
A simple test:
Introduction to ASP. 10, Action filter with built-in filter implementation (instance-anti-theft chain)