fix
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
namespace MyOffice.Web.Infrastructure.Attributes;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
||||
@@ -11,28 +8,6 @@ public sealed class DefaultFromBodyAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
public class GlobalModelStateValidatorAttribute : ActionFilterAttribute
|
||||
{
|
||||
private readonly ProblemDetailsFactory _problemDetailsFactory;
|
||||
|
||||
public GlobalModelStateValidatorAttribute(ProblemDetailsFactory problemDetailsFactory)
|
||||
{
|
||||
_problemDetailsFactory = problemDetailsFactory;
|
||||
}
|
||||
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
if (!context.ModelState.IsValid)
|
||||
{
|
||||
context.Result = new ObjectResult(_problemDetailsFactory.CreateValidationProblemDetails(context.HttpContext, context.ModelState))
|
||||
{
|
||||
StatusCode = StatusCodes.Status400BadRequest
|
||||
};
|
||||
}
|
||||
|
||||
base.OnActionExecuting(context);
|
||||
}
|
||||
}
|
||||
public class DefaultFromBodyBindingConvention : IActionModelConvention
|
||||
{
|
||||
public void Apply(ActionModel action)
|
||||
@@ -43,13 +18,16 @@ public class DefaultFromBodyBindingConvention : IActionModelConvention
|
||||
}
|
||||
|
||||
if (action.Controller.Attributes.Any(a => a is DefaultFromBodyAttribute))
|
||||
{
|
||||
{
|
||||
foreach (var parameter in action.Parameters)
|
||||
{
|
||||
var paramType = parameter.ParameterInfo.ParameterType;
|
||||
var isSimpleType = paramType.IsPrimitive
|
||||
|| paramType.IsEnum
|
||||
|| paramType == typeof(string)
|
||||
|| paramType == typeof(int)
|
||||
|| paramType == typeof(Guid)
|
||||
|| paramType == typeof(DateTime)
|
||||
|| paramType == typeof(decimal);
|
||||
|
||||
if (!isSimpleType)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace MyOffice.Web.Infrastructure.Attributes;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||
|
||||
public class GlobalModelStateValidatorAttribute : ActionFilterAttribute
|
||||
{
|
||||
private readonly ProblemDetailsFactory _problemDetailsFactory;
|
||||
|
||||
public GlobalModelStateValidatorAttribute(ProblemDetailsFactory problemDetailsFactory)
|
||||
{
|
||||
_problemDetailsFactory = problemDetailsFactory;
|
||||
}
|
||||
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
if (!context.ModelState.IsValid)
|
||||
{
|
||||
context.Result = new ObjectResult(_problemDetailsFactory.CreateValidationProblemDetails(context.HttpContext, context.ModelState))
|
||||
{
|
||||
StatusCode = StatusCodes.Status400BadRequest
|
||||
};
|
||||
}
|
||||
|
||||
base.OnActionExecuting(context);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
namespace MyOffice.Web.Infrastructure.Filters;
|
||||
|
||||
using System.Collections;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using MyOffice.Web.Models;
|
||||
|
||||
public class ResponseFilter : IActionFilter
|
||||
{
|
||||
private readonly ILogger<ResponseFilter> _logger;
|
||||
|
||||
public ResponseFilter(ILogger<ResponseFilter> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
if (context.Result == null)
|
||||
throw new NotSupportedException($"Response must be - empty");
|
||||
if (!(context.Result is ObjectResult result))
|
||||
throw new NotSupportedException($"Response must be an ObjectResult - {context.Result?.GetType().Name}");
|
||||
if (result.Value == null)
|
||||
throw new NotSupportedException($"Response must be an ObjectResult - empty");
|
||||
|
||||
if (result.Value is IResponseModel)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var type = result.Value.GetType();
|
||||
if (type.IsGenericType
|
||||
&& result.Value is IEnumerable
|
||||
&& type.GenericTypeArguments.Any(x => x.GetInterfaces().Any(y => y == typeof(IResponseModel))))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new NotSupportedException($"Response must be an ObjectResult with an IResponseModel - {result.Value?.GetType().Name}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user