@@ -19,13 +19,12 @@ namespace {{packageName}}.Attributes
1919 public override void OnActionExecuting(ActionExecutingContext context)
2020 {
2121 // Per https://blog.markvincze.com/how-to-validate-action-parameters-with-dataannotation-attributes/
22- var descriptor = context.ActionDescriptor as ControllerActionDescriptor;
23- if (descriptor != null)
22+ if (context.ActionDescriptor is ControllerActionDescriptor descriptor)
2423 {
2524 foreach (var parameter in descriptor.MethodInfo.GetParameters())
2625 {
27- object args = null;
28- if (context.ActionArguments.ContainsKey(parameter.Name))
26+ object{{#nullableReferenceTypes } }? { {/nullableReferenceTypes } } args = null;
27+ if ({ {#nullableReferenceTypes } }parameter.Name != null && { {/nullableReferenceTypes } } context.ActionArguments.ContainsKey(parameter.Name))
2928 {
3029 args = context.ActionArguments[parameter.Name];
3130 }
@@ -40,17 +39,16 @@ namespace {{packageName}}.Attributes
4039 }
4140 }
4241
43- private void ValidateAttributes(ParameterInfo parameter, object args, ModelStateDictionary modelState)
42+ private void ValidateAttributes(ParameterInfo parameter, object{ {#nullableReferenceTypes } }? { {/nullableReferenceTypes } } args, ModelStateDictionary modelState)
4443 {
4544 foreach (var attributeData in parameter.CustomAttributes)
4645 {
4746 var attributeInstance = parameter.GetCustomAttribute(attributeData.AttributeType);
4847
49- var validationAttribute = attributeInstance as ValidationAttribute;
50- if (validationAttribute != null)
48+ if (attributeInstance is ValidationAttribute validationAttribute)
5149 {
5250 var isValid = validationAttribute.IsValid(args);
53- if (! isValid)
51+ if (! isValid{{#nullableReferenceTypes } } && parameter.Name != null { {/nullableReferenceTypes } } )
5452 {
5553 modelState.AddModelError(parameter.Name, validationAttribute.FormatErrorMessage(parameter.Name));
5654 }
0 commit comments