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

Source: Internet
Author: User

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 class Antioutsitelinkattribute:actionfilterattribute, Iactionfilter
{
Public Antioutsitelinkattribute (FileType FileType)
{
This. FileType = FileType;
}

<summary>
The requested file type. (file or picture)
</summary>
Public FileType FileType{get; set;}

Iactionfilter Members

<summary>
Get the root domain name of the site
</summary>
<param name= "domain" > website, without "http.//" </param>
<returns></returns>
private string Getrootdomain (string domain)
{
if (string. IsNullOrEmpty (domain))
{
throw new ArgumentNullException ("parameter ' domain ' cannot be empty");
}
string[] arr = domain. Split (new[){'. '}, Stringsplitoptions.removeemptyentries);
if (arr. Length <= 2)
{
return domain;
}
Else
{
Return Arr[arr. Length-2] + "." + Arr[arr. LENGTH-1];
}
}
}

public enum FileType
{
File = 1,
Image
}

We then set up a controller to process the file request and apply the filter that we just created:

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");
}
}

A simple test:

The application of Actionfilter is very extensive, which requires you to use it flexibly. For other applications, you can refer to my previously translated ASP. NET MVC Action Filter-cache and compress this article.

Go 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.