How to make a Custom Attribute in a webapi get the passed parameter via C #

1

I am creating a Custom Attribute in a webapi for validation of a value and would like to know if it can capture this value from a GET request.

I wanted to do it this way:

[Validacao]
public Empresa consularEmpresa([FromUri]string codigo){}

No need to go through this:

[Validacao("codigo")]
public Empresa consultaEpresa([FromUri]string codigo){}

Validation Code:

  [AttributeUsage(AttributeTargets.All)]
public class NfeValidation:ValidationAttribute
{
    public Validacao(string chave)
    {
        if (!string.IsNullOrEmpty(chave) && chave.Length == 44)
        {
            string modeloNF = chave.Substring(18,2);
            if(modeloNF != "55")
            {
                new HttpException("Modelo de nota fiscal inválido!");
            }
        }
    }
}

That above is my example context of what I wanted to do.

For example, I need to validate a passkey passed in my webapi via url and verify that it is in the 44 digits and if the template is 55 the first one my (Attribute or ValidationAttribute) should validate this rule before executing my method

    
asked by anonymous 02.03.2018 / 15:19

1 answer

1

I was able to resolve it as follows:

    public class ValidationNF:ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        base.OnActionExecuting(actionContext);
        var descriptor = actionContext.ControllerContext.ControllerDescriptor.ControllerName
                            + " - " + actionContext.ActionDescriptor.ActionName;

        var header = actionContext.Request.Headers;
        string erro = "";

}}

And I can get the data and validate before calling the method. The call was exactly what I wanted:

    [ValidationNF]
public class ConsultaNFController : ApiController
{
    [AcceptVerbs("GET")]
    public Empresa ConsultaNfe([FromUri]string chave)
    {
        Empresa empresa = new Empresa();
        return empresa;
    }
}

Now I'll handle what I need, thank you anyway;)

    
02.03.2018 / 17:13