1
This commit is contained in:
56
iMES.Core/Utilities/Response/FilterResponse.cs
Normal file
56
iMES.Core/Utilities/Response/FilterResponse.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using iMES.Core.Enums;
|
||||
using iMES.Core.Extensions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using iMES.Core.Const;
|
||||
|
||||
namespace iMES.Core.Utilities
|
||||
{
|
||||
public static class FilterResponse
|
||||
{
|
||||
public static void GetContentResult(FilterContext context, IActionResult actionResult)
|
||||
{
|
||||
GetContentResult(context, actionResult, null);
|
||||
}
|
||||
|
||||
public static void SetActionResult(ActionExecutingContext context, WebResponseContent responseData)
|
||||
{
|
||||
context.Result = new ContentResult()
|
||||
{
|
||||
Content = new { status = false, message = responseData.Message }.Serialize(),
|
||||
ContentType = ApplicationContentType.JSON,
|
||||
StatusCode = (int)HttpStatusCode.Unauthorized
|
||||
};
|
||||
}
|
||||
|
||||
public static void GetContentResult(FilterContext context, IActionResult actionResult, WebResponseContent responseData)
|
||||
{
|
||||
responseData = responseData ?? new WebResponseContent();
|
||||
responseData.Set(ResponseType.ServerError);
|
||||
|
||||
if (context.HttpContext.IsAjaxRequest())
|
||||
{
|
||||
actionResult = new ContentResult()
|
||||
{
|
||||
Content = JsonConvert.SerializeObject(responseData),
|
||||
ContentType = ApplicationContentType.JSON,
|
||||
StatusCode = (int)HttpStatusCode.InternalServerError
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
string desc = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(responseData.Message));
|
||||
actionResult = new ContentResult()
|
||||
{
|
||||
Content = $@"<html><head><title></title></head><body>{desc}</body></html>",
|
||||
ContentType = "text/html",
|
||||
StatusCode = (int)HttpStatusCode.InternalServerError
|
||||
};
|
||||
}
|
||||
//writelog
|
||||
}
|
||||
}
|
||||
}
|
||||
83
iMES.Core/Utilities/Response/WebResponseContent.cs
Normal file
83
iMES.Core/Utilities/Response/WebResponseContent.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using iMES.Core.Enums;
|
||||
using iMES.Core.Extensions;
|
||||
|
||||
namespace iMES.Core.Utilities
|
||||
{
|
||||
public class WebResponseContent
|
||||
{
|
||||
public WebResponseContent()
|
||||
{
|
||||
}
|
||||
public WebResponseContent(bool status)
|
||||
{
|
||||
this.Status = status;
|
||||
}
|
||||
public bool Status { get; set; }
|
||||
public string Code { get; set; }
|
||||
public string Message { get; set; }
|
||||
//public string Message { get; set; }
|
||||
public object Data { get; set; }
|
||||
|
||||
public WebResponseContent OK()
|
||||
{
|
||||
this.Status = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static WebResponseContent Instance
|
||||
{
|
||||
get { return new WebResponseContent(); }
|
||||
}
|
||||
public WebResponseContent OK(string message = null,object data=null)
|
||||
{
|
||||
this.Status = true;
|
||||
this.Message = message;
|
||||
this.Data = data;
|
||||
return this;
|
||||
}
|
||||
public WebResponseContent OK(ResponseType responseType)
|
||||
{
|
||||
return Set(responseType, true);
|
||||
}
|
||||
public WebResponseContent Error(string message = null)
|
||||
{
|
||||
this.Status = false;
|
||||
this.Message = message;
|
||||
return this;
|
||||
}
|
||||
public WebResponseContent Error(ResponseType responseType)
|
||||
{
|
||||
return Set(responseType, false);
|
||||
}
|
||||
public WebResponseContent Set(ResponseType responseType)
|
||||
{
|
||||
bool? b = null;
|
||||
return this.Set(responseType, b);
|
||||
}
|
||||
public WebResponseContent Set(ResponseType responseType, bool? status)
|
||||
{
|
||||
return this.Set(responseType, null, status);
|
||||
}
|
||||
public WebResponseContent Set(ResponseType responseType, string msg)
|
||||
{
|
||||
bool? b = null;
|
||||
return this.Set(responseType, msg, b);
|
||||
}
|
||||
public WebResponseContent Set(ResponseType responseType, string msg, bool? status)
|
||||
{
|
||||
if (status != null)
|
||||
{
|
||||
this.Status = (bool)status;
|
||||
}
|
||||
this.Code = ((int)responseType).ToString();
|
||||
if (!string.IsNullOrEmpty(msg))
|
||||
{
|
||||
Message = msg;
|
||||
return this;
|
||||
}
|
||||
Message = responseType.GetMsg();
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user