Initial_commit_SecMPS_v2
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
//using VolPro.Core.Enums;
|
||||
//using VolPro.Entity.DomainModels.ResponseEntity;
|
||||
//using VolPro.Entity.SystemsModels;
|
||||
//using Microsoft.AspNetCore.Http;
|
||||
//using Newtonsoft.Json;
|
||||
//using System;
|
||||
//using System.IO;
|
||||
//using System.Net;
|
||||
//using System.Text;
|
||||
//using System.Threading.Tasks;
|
||||
//using System.Xml.Serialization;
|
||||
|
||||
//namespace VolPro.Core.Extensions.Middleware
|
||||
//{
|
||||
// public class ExceptionHandlerMiddleWare
|
||||
// {
|
||||
// private readonly RequestDelegate next;
|
||||
|
||||
// public ExceptionHandlerMiddleWare(RequestDelegate next)
|
||||
// {
|
||||
// this.next = next;
|
||||
// }
|
||||
|
||||
// public async Task Invoke(HttpContext context)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// await next(context);
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// await HandleExceptionAsync(context, ex);
|
||||
// }
|
||||
// }
|
||||
|
||||
// private static async Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||
// {
|
||||
// if (exception == null) return;
|
||||
// await WriteExceptionAsync(context, exception).ConfigureAwait(false);
|
||||
// }
|
||||
|
||||
// private static async Task WriteExceptionAsync(HttpContext context, Exception exception)
|
||||
// {
|
||||
|
||||
// Logger.WriteLog(LogEnum.Exception, null, exception.Message + exception.StackTrace, null);
|
||||
|
||||
// var response = context.Response;
|
||||
|
||||
// if (exception is UnauthorizedAccessException)
|
||||
// response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
||||
// else if (exception is Exception)
|
||||
// // response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
// response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||
// response.ContentType = context.Request.Headers["Accept"];
|
||||
|
||||
// //if (response.ContentType.ToLower() == "application/xml")
|
||||
// response.ContentType = "application/json";
|
||||
// ResponseData responseData = new ResponseData();
|
||||
// responseData.SetResponseValue(ResponseType.ServerError);
|
||||
// string reslutMsg = JsonConvert.SerializeObject(responseData);
|
||||
|
||||
// await response.WriteAsync(reslutMsg, Encoding.UTF8).ConfigureAwait(false);
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 对象转为Xml
|
||||
// /// </summary>
|
||||
// /// <param name="o"></param>
|
||||
// /// <returns></returns>
|
||||
// private static string Object2XmlString(object o)
|
||||
// {
|
||||
// StringWriter sw = new StringWriter();
|
||||
// try
|
||||
// {
|
||||
// XmlSerializer serializer = new XmlSerializer(o.GetType());
|
||||
// serializer.Serialize(sw, o);
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
// //Handle Exception Code
|
||||
// }
|
||||
// finally
|
||||
// {
|
||||
// sw.Dispose();
|
||||
// }
|
||||
// return sw.ToString();
|
||||
// }
|
||||
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,58 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace VolPro.Core.Extensions
|
||||
{
|
||||
public class HttpContextMiddleware
|
||||
{
|
||||
/// <summary>
|
||||
/// 将流重新
|
||||
/// </summary>
|
||||
public static Func<RequestDelegate, RequestDelegate> contextMiddleware
|
||||
{
|
||||
get
|
||||
{
|
||||
return next => async context =>
|
||||
{
|
||||
|
||||
var stream = context.Request.Body;
|
||||
if (stream == Stream.Null || stream.CanSeek)
|
||||
{
|
||||
await next(context);
|
||||
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
//将流拷贝一份出来,调用结束后再将流写回去
|
||||
using (var buffer = new MemoryStream())
|
||||
{
|
||||
// Copy the request stream to the memory stream.
|
||||
await stream.CopyToAsync(buffer);
|
||||
|
||||
// Rewind the memory stream.
|
||||
buffer.Position = 0L;
|
||||
|
||||
// Replace the request stream by the memory stream.
|
||||
context.Request.Body = buffer;
|
||||
|
||||
// Invoke the rest of the pipeline.
|
||||
await next(context);
|
||||
}
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
// Restore the original stream.
|
||||
context.Request.Body = stream;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user