[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class StaticFileHandlerFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext context){}
public override void OnActionExecuting(ActionExecutingContext context){}
}
//按照一定的规则生成静态文件的名称,这里是按照area+"-"+controller+"-"+action+key规则生成
string controllerName = context.RouteData.Values["controller"].ToString().ToLower();
string actionName = context.RouteData.Values["action"].ToString().ToLower();
string area = context.RouteData.Values["area"].ToString().ToLower();
//这里的Key默认等于id,当然我们可以配置不同的Key名称
string id = context.RouteData.Values.ContainsKey(Key) ? context.RouteData.Values[Key].ToString() : "";
if (string.IsNullOrEmpty(id) && context.HttpContext.Request.Query.ContainsKey(Key))
{
id = context.HttpContext.Request.Query[Key];
}
string filePath = Path.Combine(AppContext.BaseDirectory, "wwwroot", area, controllerName + "-" + actionName + (string.IsNullOrEmpty(id) ? "" : ("-" + id)) + ".html");
//判断文件是否存在
if (File.Exists(filePath))
{
//如果存在,直接读取文件
using (FileStream fs = File.Open(filePath, FileMode.Open))
{
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{
//通过contentresult返回文件内容
ContentResult contentresult = new ContentResult();
contentresult.Content = sr.ReadToEnd();
contentresult.ContentType = "text/html";
context.Result = contentresult;
}
}
}
//获取结果
IActionResult actionResult = context.Result;
//判断结果是否是一个ViewResult
if (actionResult is ViewResult)
{
ViewResult viewResult = actionResult as ViewResult;
//下面的代码就是执行这个ViewResult,并把结果的html内容放到一个StringBuiler对象中
var services = context.HttpContext.RequestServices;
var executor = services.GetRequiredService<ViewResultExecutor>();
var option = services.GetRequiredService<IOptions<MvcViewOptions>>();
var result = executor.FindView(context, viewResult);
result.EnsureSuccessful(originalLocations: null);
var view = result.View;
StringBuilder builder = new StringBuilder();
using (var writer = new StringWriter(builder))
{
var viewContext = new ViewContext(
context,
view,
viewResult.ViewData,
viewResult.TempData,
writer,
option.Value.HtmlHelperOptions);
view.RenderAsync(viewContext).GetAwaiter().GetResult();
//这句一定要调用,否则内容就会是空的
writer.Flush();
}
//按照规则生成静态文件名称
string area = context.RouteData.Values["area"].ToString().ToLower();
string controllerName = context.RouteData.Values["controller"].ToString().ToLower();
string actionName = context.RouteData.Values["action"].ToString().ToLower();
string id = context.RouteData.Values.ContainsKey(Key) ? context.RouteData.Values[Key].ToString() : "";
if (string.IsNullOrEmpty(id) && context.HttpContext.Request.Query.ContainsKey(Key))
{
id = context.HttpContext.Request.Query[Key];
}
string devicedir = Path.Combine(AppContext.BaseDirectory, "wwwroot", area);
if (!Directory.Exists(devicedir))
{
Directory.CreateDirectory(devicedir);
}
//写入文件
string filePath = Path.Combine(AppContext.BaseDirectory, "wwwroot", area, controllerName + "-" + actionName + (string.IsNullOrEmpty(id) ? "" : ("-" + id)) + ".html");
using (FileStream fs = File.Open(filePath, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
sw.Write(builder.ToString());
}
}
//输出当前的结果
ContentResult contentresult = new ContentResult();
contentresult.Content = builder.ToString();
contentresult.ContentType = "text/html";
context.Result = contentresult;
}
public string Key
{
get;set;
}
//获取文件信息对象
FileInfo fileInfo=new FileInfo(filePath);
//结算时间间隔,如果小于等于两分钟,就直接输出,当然这里的规则可以改
TimeSpan ts = DateTime.Now - fileInfo.CreationTime;
if(ts.TotalMinutes<=2)
{
using (FileStream fs = File.Open(filePath, FileMode.Open))
{
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{
ContentResult contentresult = new ContentResult();
contentresult.Content = sr.ReadToEnd();
contentresult.ContentType = "text/html";
context.Result = contentresult;
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有