31 lines
1009 B
C#
31 lines
1009 B
C#
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Text.Json.Serialization;
|
|
using System.Text.Json;
|
|
|
|
namespace ServiceHost.MiddleWare;
|
|
|
|
public class ApiJsonEnumFilter : IActionFilter
|
|
{
|
|
public void OnActionExecuting(ActionExecutingContext context)
|
|
{
|
|
// قبل از اکشن نیازی نیست کاری کنیم
|
|
}
|
|
|
|
public void OnActionExecuted(ActionExecutedContext context)
|
|
{
|
|
if (context.Result is ObjectResult objectResult)
|
|
{
|
|
var enumConverter = new JsonStringEnumConverter();
|
|
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
|
|
options.Converters.Add(enumConverter);
|
|
var json = JsonSerializer.Serialize(objectResult.Value, options);
|
|
context.Result = new ContentResult
|
|
{
|
|
Content = json,
|
|
ContentType = "application/json",
|
|
StatusCode = objectResult.StatusCode
|
|
};
|
|
}
|
|
}
|
|
} |