OnActionExecuting ValidationAttribute

Viewed 44

I am trying to get value of the parameter from action method, instead of underlined value, should not there be key?(x.key) in order to get the argument name?

var param = context.ActionArguments
  .SingleOrDefault(x => x.Value.ToString().Contains("DTO")).Value;

[HttpPost]
[ServiceFilter(typeof(ValidationFilterAttribute))]
public async Task<IActionResult> CreateCompany([FromBody] CompanyForCreationDTO company)
1 Answers

Please try this way, It will bring the parameter value of a controller which has been passed.

public void OnActionExecuting(ActionExecutingContext context)
        {
            var descriptor = context.ActionDescriptor as ControllerActionDescriptor;

            if (descriptor != null)
            {
                var parameters = descriptor.MethodInfo.GetParameters();

                foreach (var parameter in parameters)
                {
                    var argument = context.ActionArguments[parameter.Name];
                }
            }
            
        }

Output:

enter image description here

You can get more info in official document here

Related