I have an custom action filter for check action parameters before execute the action
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (context.ModelState.IsValid == false)
throw new Exception("");
if (context.ActionArguments.Values.Any() && context.ActionArguments.Values.All(v => v.IsAllPropertiesNull()))
throw new Exception("");
await next();
}
how can I check the context.ActionArguments.Value
is [FromBody]
or [FromServices]
or [FromRoute]
and etc...
You get the binding source from the BindingInfo
of each parameter. You get this from the context.ActionDescriptor.Parameters
. Here is an example.
public class CustomActionFilter: IAsyncActionFilter
{
/// <inheritdoc />
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
foreach (var parameterDescriptor in context.ActionDescriptor.Parameters)
{
var bindingSource = parameterDescriptor.BindingInfo.BindingSource;
if (bindingSource == BindingSource.Body)
{
// bound from body
}
else if (bindingSource == BindingSource.Services)
{
// from services
}
else if (bindingSource == BindingSource.Query)
{
// from query string
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.