What is the name of these parameters in ASP.NET MVC?

Asked

Viewed 119 times

2

What is the name of these parameters between brackets used to restrict some access or define the protocol used, as in the example: [Authorize] and [HttpPost].

Is it possible to create a custom "filter"? to, for example, allow access to users with a Role specific.

Código no Visual Studio

  • Ask 3 questions I’m talking about writing these filters: http://answall.com/search?q=user%3A2999+Authorize

2 answers

6


These constructions are attributes, are not parameters. Part of the syntax is C#. The attribute is just information, called metadata. It alone does nothing. there needs to be a mechanism in the code that reads it and does something.

The ones you are seeing are still personalized. It was ASP.NET MVC that created them, in this case they are called action Filters.

You can create yours as well by following a few rules. Obviously there needs to be a reason to create one and have some mechanism that consumes them.

2

You do not need to create a new attribute to limit the authorization by ROLE. You can use Authorize itself, for example:

[Authorize(Roles="User")]

Now if you’d really like to create a new attribute, here’s some information.

You can create a custom attribute as follows:

public class Author : System.Attribute
{
   private string name;
   public double version;

   public Author(string name)
   {
       this.name = name;
       version = 1.0;
   }
}

[Author("P. Ackerman", version = 1.1)]
class SampleClass
{
    // P. Ackerman's code goes here...
}

But the attribute alone does nothing. You need to create a Reflection routine to identify the attributes and give them some meaning. Below I quote a reference link that explains better.

Reference: https://msdn.microsoft.com/en-us/library/sw480ze8.aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.