mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-07 02:00:51 +08:00
框架分层
This commit is contained in:
8
Yi.Framework/Yi.Framework.WebCore/Class1.cs
Normal file
8
Yi.Framework/Yi.Framework.WebCore/Class1.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace Yi.Framework.WebCore
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
}
|
||||
}
|
||||
36
Yi.Framework/Yi.Framework.WebCore/CommonExtend.cs
Normal file
36
Yi.Framework/Yi.Framework.WebCore/CommonExtend.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using CC.ElectronicCommerce.Model;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CC.ElectronicCommerce.WebCore
|
||||
{
|
||||
public static class CommonExtend
|
||||
{
|
||||
public static bool IsAjaxRequest(this HttpRequest request)
|
||||
{
|
||||
string header = request.Headers["X-Requested-With"];
|
||||
return "XMLHttpRequest".Equals(header);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 基于HttpContext,当前鉴权方式解析,获取用户信息
|
||||
/// </summary>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <returns></returns>
|
||||
public static UserInfo GetCurrentUserInfo(this HttpContext httpContext)
|
||||
{
|
||||
IEnumerable<Claim> claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
|
||||
return new UserInfo()
|
||||
{
|
||||
id = long.Parse(claimlist.FirstOrDefault(u => u.Type == "id").Value),
|
||||
username = claimlist.FirstOrDefault(u => u.Type == "username").Value ?? "匿名"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Yi.Framework/Yi.Framework.WebCore/FilterExtend/CORSFilter.cs
Normal file
18
Yi.Framework/Yi.Framework.WebCore/FilterExtend/CORSFilter.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
//using Microsoft.AspNetCore.Mvc.Filters;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace Zhaoxi.AgileFramework.WebCore.FilterExtend
|
||||
//{
|
||||
// public class CORSFilter : ActionFilterAttribute
|
||||
// {
|
||||
// public override void OnActionExecuting(ActionExecutingContext context)
|
||||
// {
|
||||
// context.HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
|
||||
// context.HttpContext.Response.Headers.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE,PUT");
|
||||
// context.HttpContext.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,78 @@
|
||||
using CC.ElectronicCommerce.Common.Models;
|
||||
using CC.ElectronicCommerce.Core;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CC.ElectronicCommerce.WebCore.FilterExtend
|
||||
{
|
||||
public class CustomAction2CommitFilterAttribute : ActionFilterAttribute
|
||||
{
|
||||
#region Identity
|
||||
private readonly ILogger<CustomAction2CommitFilterAttribute> _logger;
|
||||
private readonly CacheClientDB _cacheClientDB;
|
||||
private static string KeyPrefix = "2CommitFilter";
|
||||
|
||||
public CustomAction2CommitFilterAttribute(ILogger<CustomAction2CommitFilterAttribute> logger, CacheClientDB cacheClientDB)
|
||||
{
|
||||
this._logger = logger;
|
||||
this._cacheClientDB = cacheClientDB;
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 防重复提交周期 单位秒
|
||||
/// </summary>
|
||||
public int TimeOut = 3;
|
||||
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
string url = context.HttpContext.Request.Path.Value;
|
||||
string argument = JsonConvert.SerializeObject(context.ActionArguments);
|
||||
string ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
|
||||
string agent = context.HttpContext.Request.Headers["User-Agent"];
|
||||
string sInfo = $"{url}-{argument}-{ip}-{agent}";
|
||||
string summary = MD5Helper.MD5EncodingOnly(sInfo);
|
||||
|
||||
string totalKey = $"{KeyPrefix}-{summary}";
|
||||
|
||||
string result = this._cacheClientDB.Get<string>(totalKey);
|
||||
if (string.IsNullOrEmpty(result))
|
||||
{
|
||||
this._cacheClientDB.Add(totalKey, "1", TimeSpan.FromSeconds(3));//3秒有效期
|
||||
this._logger.LogInformation($"CustomAction2CommitFilterAttribute:{sInfo}");
|
||||
}
|
||||
else
|
||||
{
|
||||
//已存在
|
||||
this._logger.LogWarning($"CustomAction2CommitFilterAttribute重复请求:{sInfo}");
|
||||
context.Result = new JsonResult(Result.Error("请勿重复提交"));
|
||||
}
|
||||
|
||||
//CurrentUser currentUser = context.HttpContext.GetCurrentUserBySession();
|
||||
//if (currentUser == null)
|
||||
//{
|
||||
// //if (this.IsAjaxRequest(context.HttpContext.Request))
|
||||
// //{ }
|
||||
// context.Result = new RedirectResult("~/Fourth/Login");
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// this._logger.LogDebug($"{currentUser.Name} 访问系统");
|
||||
//}
|
||||
}
|
||||
private bool IsAjaxRequest(HttpRequest request)
|
||||
{
|
||||
string header = request.Headers["X-Requested-With"];
|
||||
return "XMLHttpRequest".Equals(header);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//using Microsoft.AspNetCore.Mvc.Filters;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace Zhaoxi.AgileFramework.WebCore.FilterExtend
|
||||
//{
|
||||
// public class CustomActionCacheFilterAttribute : ActionFilterAttribute
|
||||
// {
|
||||
// public override void OnActionExecuted(ActionExecutedContext context)
|
||||
// {
|
||||
// context.HttpContext.Response.Headers.Add("Cache-Control", "public,max-age=6000");
|
||||
// Console.WriteLine($"This {nameof(CustomActionCacheFilterAttribute)} OnActionExecuted{this.Order}");
|
||||
// }
|
||||
// public override void OnActionExecuting(ActionExecutingContext context)
|
||||
// {
|
||||
// Console.WriteLine($"This {nameof(CustomActionCacheFilterAttribute)} OnActionExecuting{this.Order}");
|
||||
// }
|
||||
// public override void OnResultExecuting(ResultExecutingContext context)
|
||||
// {
|
||||
// Console.WriteLine($"This {nameof(CustomActionCacheFilterAttribute)} OnResultExecuting{this.Order}");
|
||||
// }
|
||||
// public override void OnResultExecuted(ResultExecutedContext context)
|
||||
// {
|
||||
// Console.WriteLine($"This {nameof(CustomActionCacheFilterAttribute)} OnResultExecuted{this.Order}");
|
||||
// }
|
||||
// }
|
||||
|
||||
//}
|
||||
@@ -0,0 +1,45 @@
|
||||
//using Microsoft.AspNetCore.Http;
|
||||
//using Microsoft.AspNetCore.Mvc;
|
||||
//using Microsoft.AspNetCore.Mvc.Filters;
|
||||
//using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
//using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
||||
//using Microsoft.Extensions.Logging;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace Zhaoxi.AgileFramework.WebCore.FilterExtend
|
||||
//{
|
||||
// public class CustomActionCheckFilterAttribute : ActionFilterAttribute
|
||||
// {
|
||||
// #region Identity
|
||||
// private readonly ILogger<CustomActionCheckFilterAttribute> _logger;
|
||||
// private readonly IModelMetadataProvider _modelMetadataProvider;
|
||||
// public CustomActionCheckFilterAttribute(ILogger<CustomActionCheckFilterAttribute> logger)
|
||||
// {
|
||||
// this._logger = logger;
|
||||
// }
|
||||
// #endregion
|
||||
|
||||
// public override void OnActionExecuting(ActionExecutingContext context)
|
||||
// {
|
||||
// //CurrentUser currentUser = context.HttpContext.GetCurrentUserBySession();
|
||||
// //if (currentUser == null)
|
||||
// //{
|
||||
// // //if (this.IsAjaxRequest(context.HttpContext.Request))
|
||||
// // //{ }
|
||||
// // context.Result = new RedirectResult("~/Fourth/Login");
|
||||
// //}
|
||||
// //else
|
||||
// //{
|
||||
// // this._logger.LogDebug($"{currentUser.Name} 访问系统");
|
||||
// //}
|
||||
// }
|
||||
// private bool IsAjaxRequest(HttpRequest request)
|
||||
// {
|
||||
// string header = request.Headers["X-Requested-With"];
|
||||
// return "XMLHttpRequest".Equals(header);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using CC.ElectronicCommerce.Common.Models;
|
||||
|
||||
namespace CC.ElectronicCommerce.WebCore.FilterExtend
|
||||
{
|
||||
public class CustomExceptionFilterAttribute : IExceptionFilter
|
||||
{
|
||||
private ILogger<CustomExceptionFilterAttribute> _logger = null;
|
||||
public CustomExceptionFilterAttribute(ILogger<CustomExceptionFilterAttribute> logger)
|
||||
{
|
||||
this._logger = logger;
|
||||
}
|
||||
|
||||
public void OnException(ExceptionContext context)
|
||||
{
|
||||
if (context.ExceptionHandled == false)
|
||||
{
|
||||
context.Result = new JsonResult(
|
||||
Result.Error("操作失败").SetData(context.Exception.Message));
|
||||
string url = context.HttpContext.Request.Path.Value;
|
||||
string actionName = context.ActionDescriptor.DisplayName;
|
||||
|
||||
var logModel = new LogModel()
|
||||
{
|
||||
OriginalClassName = "",
|
||||
OriginalMethodName = actionName,
|
||||
Remark = $"来源于{nameof(CustomExceptionFilterAttribute)}.{nameof(OnException)}"
|
||||
};
|
||||
this._logger.LogError(context.Exception, $"{url}----->actionName={actionName} Message={context.Exception.Message}", JsonConvert.SerializeObject(logModel));
|
||||
}
|
||||
context.ExceptionHandled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
//using Microsoft.AspNetCore.Mvc.Filters;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace Zhaoxi.AgileFramework.WebCore.FilterExtend
|
||||
//{
|
||||
// /// <summary>
|
||||
// /// 基于完成Filter的依赖注入
|
||||
// /// </summary>
|
||||
// public class CustomIOCFilterFactoryAttribute : Attribute, IFilterFactory
|
||||
// {
|
||||
// private readonly Type _FilterType = null;
|
||||
|
||||
// public CustomIOCFilterFactoryAttribute(Type type)
|
||||
// {
|
||||
// this._FilterType = type;
|
||||
// }
|
||||
// public bool IsReusable => true;
|
||||
|
||||
// public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
|
||||
// {
|
||||
// //return (IFilterMetadata)serviceProvider.GetService(typeof(CustomExceptionFilterAttribute));
|
||||
|
||||
// return (IFilterMetadata)serviceProvider.GetService(this._FilterType);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
//}
|
||||
@@ -0,0 +1,45 @@
|
||||
//using Microsoft.AspNetCore.Mvc;
|
||||
//using Microsoft.AspNetCore.Mvc.Filters;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace Zhaoxi.AgileFramework.WebCore.FilterExtend
|
||||
//{
|
||||
// /// <summary>
|
||||
// ///
|
||||
// /// </summary>
|
||||
// public class CustomResourceFilterAttribute : Attribute, IResourceFilter, IFilterMetadata
|
||||
// {
|
||||
// private static Dictionary<string, IActionResult> CustomCache = new Dictionary<string, IActionResult>();
|
||||
// /// <summary>
|
||||
// /// 发生在其他动作之前
|
||||
// /// </summary>
|
||||
// /// <param name="context"></param>
|
||||
// public void OnResourceExecuting(ResourceExecutingContext context)
|
||||
// {
|
||||
// Console.WriteLine($"This is {nameof(CustomResourceFilterAttribute) }OnResourceExecuting");
|
||||
// //if 有缓存,直接返回缓存
|
||||
// string key = context.HttpContext.Request.Path;
|
||||
// if (CustomCache.ContainsKey(key))
|
||||
// {
|
||||
// context.Result = CustomCache[key];//断路器--到Result生成了,但是Result还需要转换成Html
|
||||
// }
|
||||
// }
|
||||
// /// <summary>
|
||||
// /// 发生在其他动作之后
|
||||
// /// </summary>
|
||||
// /// <param name="context"></param>
|
||||
// public void OnResourceExecuted(ResourceExecutedContext context)
|
||||
// {
|
||||
// Console.WriteLine($"This is {nameof(CustomResourceFilterAttribute) }OnResourceExecuted");
|
||||
// //这个应该缓存起来
|
||||
// string key = context.HttpContext.Request.Path;
|
||||
// if (!CustomCache.ContainsKey(key))
|
||||
// {
|
||||
// CustomCache.Add(key, context.Result);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,80 @@
|
||||
using CC.ElectronicCommerce.Common.Models;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CC.ElectronicCommerce.WebCore.FilterExtend
|
||||
{
|
||||
public class LogActionFilterAttribute : ActionFilterAttribute
|
||||
{
|
||||
private ILogger<LogActionFilterAttribute> _logger = null;
|
||||
public LogActionFilterAttribute(ILogger<LogActionFilterAttribute> logger)
|
||||
{
|
||||
this._logger = logger;
|
||||
}
|
||||
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
string url = context.HttpContext.Request.Path.Value;
|
||||
string argument = JsonConvert.SerializeObject(context.ActionArguments);
|
||||
|
||||
string controllerName = context.Controller.GetType().FullName;
|
||||
string actionName = context.ActionDescriptor.DisplayName;
|
||||
|
||||
LogModel logModel = new LogModel()
|
||||
{
|
||||
OriginalClassName = controllerName,
|
||||
OriginalMethodName = actionName,
|
||||
Remark = $"来源于{nameof(LogActionFilterAttribute)}.{nameof(OnActionExecuting)}"
|
||||
};
|
||||
|
||||
//this._logger.LogInformation($"url={url}---argument={argument}",new object[] { JsonConvert.SerializeObject(logModel) } );
|
||||
this._logger.LogInformation($"url={url}---argument={argument}");
|
||||
}
|
||||
}
|
||||
|
||||
public class CustomActionFilterAttribute : ActionFilterAttribute
|
||||
{
|
||||
public override void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
Console.WriteLine($"This {nameof(LogActionFilterAttribute)} OnActionExecuted{this.Order}");
|
||||
}
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
Console.WriteLine($"This {nameof(LogActionFilterAttribute)} OnActionExecuting{this.Order}");
|
||||
}
|
||||
public override void OnResultExecuting(ResultExecutingContext context)
|
||||
{
|
||||
Console.WriteLine($"This {nameof(LogActionFilterAttribute)} OnResultExecuting{this.Order}");
|
||||
}
|
||||
public override void OnResultExecuted(ResultExecutedContext context)
|
||||
{
|
||||
Console.WriteLine($"This {nameof(LogActionFilterAttribute)} OnResultExecuted{this.Order}");
|
||||
}
|
||||
}
|
||||
|
||||
public class CustomControllerFilterAttribute : ActionFilterAttribute
|
||||
{
|
||||
public override void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
Console.WriteLine($"This {nameof(CustomControllerFilterAttribute)} OnActionExecuted {this.Order}");
|
||||
}
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
Console.WriteLine($"This {nameof(CustomControllerFilterAttribute)} OnActionExecuting{this.Order}");
|
||||
}
|
||||
public override void OnResultExecuting(ResultExecutingContext context)
|
||||
{
|
||||
Console.WriteLine($"This {nameof(CustomControllerFilterAttribute)} OnResultExecuting{this.Order}");
|
||||
}
|
||||
public override void OnResultExecuted(ResultExecutedContext context)
|
||||
{
|
||||
Console.WriteLine($"This {nameof(CustomControllerFilterAttribute)} OnResultExecuted{this.Order}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CC.ElectronicCommerce.WebCore.MiddlewareExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// Axios会触发,需要做个状态返回,还需要指定跨域信息,这里放在网关了
|
||||
///
|
||||
/// OPTIONS请求即预检请求,可用于检测服务器允许的http方法。当发起跨域请求时,由于安全原因,触发一定条件时浏览器会在正式请求之前自动先发起OPTIONS请求,即CORS预检请求,服务器若接受该跨域请求,浏览器才继续发起正式请求。
|
||||
/// </summary>
|
||||
public class PreOptionRequestMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public PreOptionRequestMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
if (context.Request.Method.ToUpper() == "OPTIONS")
|
||||
{
|
||||
//context.Response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:8070");
|
||||
//context.Response.Headers.Add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,PATCH,OPTIONS");
|
||||
//context.Response.Headers.Add("Access-Control-Allow-Headers", "*");
|
||||
context.Response.StatusCode = 200;
|
||||
return;
|
||||
}
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扩展中间件
|
||||
/// </summary>
|
||||
public static class PreOptionsRequestMiddlewareExtensions
|
||||
{
|
||||
public static IApplicationBuilder UsePreOptionsRequest(this IApplicationBuilder app)
|
||||
{
|
||||
return app.UseMiddleware<PreOptionRequestMiddleware>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CC.ElectronicCommerce.WebCore.MiddlewareExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// 支持在返回HTML时,将返回的Stream保存到指定目录
|
||||
/// </summary>
|
||||
public class StaticPageMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private string _directoryPath = @"D:/cc-ec/";
|
||||
private bool _supportDelete = false;
|
||||
private bool _supportWarmup = false;
|
||||
|
||||
public StaticPageMiddleware(RequestDelegate next, string directoryPath, bool supportDelete, bool supportWarmup)
|
||||
{
|
||||
this._next = next;
|
||||
this._directoryPath = directoryPath;
|
||||
this._supportDelete = supportDelete;
|
||||
this._supportWarmup = supportWarmup;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
if (this._supportDelete && "Delete".Equals(context.Request.Query["ActionHeader"]))
|
||||
{
|
||||
this.DeleteHmtl(context.Request.Path.Value);
|
||||
context.Response.StatusCode = 200;
|
||||
}
|
||||
else if (this._supportWarmup && "ClearAll".Equals(context.Request.Query["ActionHeader"]))
|
||||
{
|
||||
this.ClearDirectory(10);//考虑数据量
|
||||
context.Response.StatusCode = 200;
|
||||
}
|
||||
else if (!context.Request.IsAjaxRequest())
|
||||
{
|
||||
Console.WriteLine($"This is StaticPageMiddleware InvokeAsync {context.Request.Path.Value}");
|
||||
#region context.Response.Body
|
||||
var originalStream = context.Response.Body;
|
||||
using (var copyStream = new MemoryStream())
|
||||
{
|
||||
context.Response.Body = copyStream;
|
||||
await _next(context);
|
||||
|
||||
copyStream.Position = 0;
|
||||
var reader = new StreamReader(copyStream);
|
||||
var content = await reader.ReadToEndAsync();
|
||||
string url = context.Request.Path.Value;
|
||||
|
||||
this.SaveHmtl(url, content);
|
||||
|
||||
copyStream.Position = 0;
|
||||
await copyStream.CopyToAsync(originalStream);
|
||||
context.Response.Body = originalStream;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
else
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveHmtl(string url, string html)
|
||||
{
|
||||
try
|
||||
{
|
||||
//Console.WriteLine($"Response: {html}");
|
||||
if (string.IsNullOrWhiteSpace(html))
|
||||
return;
|
||||
if (!url.EndsWith(".html"))
|
||||
return;
|
||||
|
||||
if (Directory.Exists(_directoryPath) == false)
|
||||
Directory.CreateDirectory(_directoryPath);
|
||||
|
||||
var totalPath = Path.Combine(_directoryPath, url.Split("/").Last());
|
||||
File.WriteAllText(totalPath, html);//直接覆盖
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除某个页面
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="index"></param>
|
||||
private void DeleteHmtl(string url)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!url.EndsWith(".html"))
|
||||
return;
|
||||
var totalPath = Path.Combine(_directoryPath, url.Split("/").Last());
|
||||
File.Delete(totalPath);//直接删除
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Delete {url} 异常,{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理文件,支持重试
|
||||
/// </summary>
|
||||
/// <param name="index">最多重试次数</param>
|
||||
private void ClearDirectory(int index)
|
||||
{
|
||||
if (index > 0)//简陋版---重试index次
|
||||
{
|
||||
try
|
||||
{
|
||||
var files = Directory.GetFiles(_directoryPath);
|
||||
foreach (var file in files)
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"ClearDirectory failed {ex.Message}");
|
||||
ClearDirectory(index--);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扩展中间件
|
||||
/// </summary>
|
||||
public static class StaticPageMiddlewareExtensions
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="directoryPath">文件写入地址,文件夹目录</param>
|
||||
/// <param name="supportDelete">是否支持删除</param>
|
||||
/// <param name="supportClear">是否支持全量删除</param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseStaticPageMiddleware(this IApplicationBuilder app, string directoryPath, bool supportDelete, bool supportClear)
|
||||
{
|
||||
return app.UseMiddleware<StaticPageMiddleware>(directoryPath, supportDelete, supportClear);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user