What is the [Httpget] statement for?

Asked

Viewed 974 times

0

In a certain method of mine Controller raised in the Asp Net MVC, I learned that I could put the statement [HttpGet].

In other cases, as in my models, I realized that can be put too [Length(10)] and other things.

I’m a little familiar with that in the Python (to declare things "on top" of the method).

For example:

 @app.route('/')
 def home(response):
     return template(response, 'meu_template.html')

In Python This is called Decorator.

And in C#? This statement made upon the method serves the same purpose as the decorators of the Python?

What is the name of this Syntax?

References:

  • 2

    http://answall.com/questions/119024/como-criar-um-custom-attribute

  • @Gabrielkatakura I think that answers the question, see

  • This in C# is called attribute.

  • 1

    Unfortunately I don’t have time at the moment to formulate an answer, and I don’t like to give answers simply with links.

  • -1 Why is the question duplicated? Duplicate is not useful? Does not reference another question? Ok, right

1 answer

2


Wallace, the [HttpGet] and the [Length] sane Attributes, which are how to implement the Decorator Pattern in the .NET.

They are useful when you want to associate some additional information to your class, field, property, etc.

If you want to implement your own Attribute, simply create a class that you inherit directly or indirectly from System.Attribute, as in the examples below.

[AttributeUsage(AttributeTargets.Class)]
public class ClassInfoAttribute : System.Attribute 
{    
   public string Namespace { get; set; }
   public string Name { get; set; }

   public ClassInfoAttribute()
   {
   }
}

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class FieldInfoAttribute : System.Attribute 
{    
   public string DataType { get; set; }
   public string Name { get; set; }

   public FieldInfoAttribute()
   {
   }
}

for uses, simply assign an attribute to its class, field, property with the name of the attribute (without the attribute).

[ClassInfo(Namespace = "Meu Namespace", Name = "Minha Classe")]
public class MinhaClasse
{    
   [FieldInfo(DataType = "Texto", Name = "Minha Propriedade")]
   public string MinhaPropriedade { get; set; }

   [FieldInfo(DataType = "Texto", Name = "Meu Campo")]
   public string meuCampo;

   public MinhaClasse()
   {
   }
}

if you need to retrieve the attribute, you will need to use a little System.Reflection

var classe = new MinhaClasse();
var tipo = classe.GetType();
var campo = tipo.GetField("meuCampo");
var propriedade = tipo.GetProperty("MinhaPropriedade");
var classIndo = (ClassInfoAttribute)tipo.GetCustomAttributes(typeof(ClassInfoAttribute), false);
var fieldInfo = (FieldInfoAttribute)campo.GetCustomAttributes(typeof(FieldInfoAttribute), false);
var propertyInfo = (FieldInfoAttribute)propriedade.GetCustomAttributes(typeof(FieldInfoAttribute), false);

Since you mentioned the ASP.NET MVC, an attribute that perhaps it is good for you to know are the Action Filter, for this it is enough to implement the interface IActionFilter and inherit from ActionFilterAttribute.

public class MeuFiltroAttribute : ActionFilterAttribute, IActionFilter
{
    void OnActionExecuting(ActionExecutingContext filterContext) { ... }
    void OnActionExecuted(ActionExecutingContext filterContext) { ... }
    void OnResultExecuting(ResultExecutingContext filterContext) { ... }
    void OnResultExecuted(ResultExecutedContext filterContext) { ... }  
}

[MeuFiltro]
public class MinhaController : Controller
{
    ...
}
  • So you implement a atributo and not a function as is done in Python?

  • This is actually a decorator, but nothing to do with https://en.wikipedia.org/wiki/Decorator_pattern. It is possible to implement.

Browser other questions tagged

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