Introduction to ASP. 10, Action filter with built-in filter implementation (instance-anti-theft chain)

Source: Internet
Author: User

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)

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.