I need to make a complex type parameter optional in 'Web API controller action', so that my action filter for null values can ignore it by checking the argument's property IsOptional
. I'm getting this error:
Optional parameter 'errorCode' is not supported by 'FormatterParameterBinding'.
I have a controller like this:
[Route("api/v1/Temp/{number}/{errorCode?}/{value?}")]
[HttpGet]
public IHttpActionResult Temp(int number, ErrorDetail errorCode = null, string value = null)
{
return Ok("good");
}
and have actionFilter to check null values like this:
public override void OnActionExecuting(HttpActionContext actionContext)
{
var parameters = actionContext.ActionDescriptor.GetParameters();
foreach (var param in parameters)
{
if (param.IsOptional)
continue;
object value = null;
if (actionContext.ActionArguments.ContainsKey(param.ParameterName))
value = actionContext.ActionArguments[param.ParameterName];
if (value == null)
throw new GasException((ulong)ErrorCodes.NullValue, ErrorCodes.NullValue.GetDescription());
}
}
Is there any way to make complex-types optional parameter? How to ignore complex-type optional parameters in action-filter?