Last active
April 21, 2016 18:26
-
-
Save Ciantic/4f211f37616de6cca3d824751b0dac79 to your computer and use it in GitHub Desktop.
Throw an error if null is passed to action. ASP.NET Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Microsoft.AspNetCore.Mvc; | |
| using Microsoft.AspNetCore.Mvc.Filters; | |
| namespace Example | |
| { | |
| public class NullValidationFilter : Attribute, IActionFilter | |
| { | |
| public void OnActionExecuting(ActionExecutingContext context) | |
| { | |
| foreach (var k in context.ActionArguments.Keys) { | |
| object o; | |
| context.ActionArguments.TryGetValue(k, out o); | |
| if (o == null) { | |
| context.Result = new BadRequestResult(); | |
| } | |
| } | |
| } | |
| public void OnActionExecuted(ActionExecutedContext context) {} | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Microsoft.AspNetCore.Mvc; | |
| using Microsoft.AspNetCore.Mvc.Filters; | |
| namespace Example | |
| { | |
| public class NullValidationFilter : Attribute, IActionFilter | |
| { | |
| public void OnActionExecuting(ActionExecutingContext context) | |
| { | |
| if (context.ActionArguments.Values.Contains(null)) { | |
| context.Result = new BadRequestResult(); | |
| } | |
| } | |
| public void OnActionExecuted(ActionExecutedContext context) {} | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I hereby place this to public domain.