Removing duplicated lines - wip
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Core.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates that ModelState validation is handled automatically by an Action Filter.
|
||||
/// Used to suppress SonarCloud warnings about missing ModelState.IsValid checks.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public class ModelStateValidationHandledByFilterAttribute : Attribute
|
||||
{ }
|
||||
}
|
||||
30
MainProject/Core/Filters/ValidationActionFilter.cs
Normal file
30
MainProject/Core/Filters/ValidationActionFilter.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Core.Filters
|
||||
{
|
||||
public class ValidationActionFilter : IAsyncActionFilter
|
||||
{
|
||||
private readonly string _requestNotWellFormedMessage = "Request is not well formed";
|
||||
|
||||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||
{
|
||||
if (!context.ModelState.IsValid)
|
||||
{
|
||||
context.Result = new BadRequestObjectResult(new { message = _requestNotWellFormedMessage, errors = context.ModelState });
|
||||
return;
|
||||
}
|
||||
|
||||
var requestBody = context.ActionArguments.Values.FirstOrDefault(arg => arg != null && !arg.GetType().IsPrimitive && !(arg is string));
|
||||
|
||||
if (requestBody == null)
|
||||
{
|
||||
context.Result = new BadRequestObjectResult(new { message = _requestNotWellFormedMessage });
|
||||
return;
|
||||
}
|
||||
|
||||
await next();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user