I'm using web api filter to validate all incoming view models and return view state error if it's null:
public class ValidateViewModelAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if(actionContext.ActionArguments != null) { foreach (var argument in actionContext.ActionArguments) { if (argument.Value != null) continue; var argumentBinding = actionContext.ActionDescriptor?.ActionBinding.ParameterBindings .FirstOrDefault(pb => pb.Descriptor.ParameterName == argument.Key); if(argumentBinding?.Descriptor?.IsOptional ?? true) continue; actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, string.Format("Arguments value for {0} cannot be null", argument.Key)); return; } } if (actionContext.ModelState.IsValid == false) { actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState); } } } I have web api working in production and now I got new request to add one optional parameter to one action. Optional.... to keep api compatibility
[Route("applyorder/{orderId}")] [HttpPost] public async Task<IHttpActionResult> ApplyOrder(int orderId, [FromBody] ApplyOrderViewModel input = null) and if I don't specify input = null it isn't considered to be an optional parameters and couldn't pass my validation. With = null I'm getting the following error:
"Message": "An error has occurred.", "ExceptionMessage": "Optional parameter 'input' is not supported by 'FormatterParameterBinding'.",
"ExceptionType": "System.InvalidOperationException", "StackTrace": " at System.Web.Http.Controllers.HttpActionBinding.ExecuteBindingAsync(
How can I keep my global viewmodel validation in place and still mark this only method parameter to be optional.
- ps: I cannot use syntax route with
?sign because it's [FromBody] - pss: I wouldn't like to introduce v2 api because it isn't a v2 api, I'm adding new optional parameter
- psss: I need some kind of attribute to update binding descriptor and specify that my parameter is optional, then it'll pass my validation.