This article mainly introduces the relevant information of asp.net core mvc to implement pseudo-static function in detail. It has certain reference value. Interested friends can refer to it
In large website systems In order to improve system access performance, some infrequent content is often published as static pages, such as the product details page and news details page of the mall. Once this information is published, the frequency of changes will not be very high. If you still use If processed through dynamic output, it will definitely cause a huge waste of resources on the server. But we cannot independently create static pages for these contents, so we can use pseudo-static processing in the system. As for what pseudo-static is, you can search it on Baidu. Let's introduce here how to implement pseudo-static in asp.net core mvc.
In the mvc framework, view represents the view, and the result of its execution is the content ultimately output to the client browser, including html, css, js, etc. If we want to achieve staticization, we need to save the result of view execution as a static file and save it to a specified location, such as disk, distributed cache, etc., so that we can directly read the saved content the next time we access it. No need to execute business logic again. So what should asp.net core mvc do to achieve such a function? The answer is to use filters. In the MVC framework, multiple filter types are provided. Here we are going to use action filters. Action filters provide two time points: before the action is executed and after the action is executed. We can first determine whether the static page has been generated before the action is executed. If it has been generated, we can directly read the file content and output it, and the subsequent logic will be skipped. If there is no production, continue to go down, capture the results at this stage after the action is executed, and then save the static content generated by the results.
Then let’s come to the specific implementation code. First, we define a filter type, which we call StaticFileHandlerFilterAttribute. This class is derived from the ActionFilterAttribute provided in the framework. StaticFileHandlerFilterAttribute overrides the two methods provided by the base class: OnActionExecuted( After the action is executed), OnActionExecuting (before the action is executed), the specific code is as follows:
[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){} }
In OnActionExecuting, you need to determine whether the static content has been generated. If it has Generate direct output content, the logic implementation is as follows:
//按照一定的规则生成静态文件的名称,这里是按照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; } } }
In OnActionExecuted, we need the action result to determine whether the action result type is a ViewResult. If it is through code Execute this result, obtain the result output, and generate a static page according to the same rules as above. The specific implementation is as follows
//获取结果 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; }
For the Key mentioned above, we directly add the corresponding Attribute
public string Key { get;set; }
So we can use this filter, the method used: add the [StaticFileHandlerFilter] attribute to the controller or controller method, if If you want to configure different Keys, you can use [StaticFileHandlerFilter(Key="set value")]
Staticization has been implemented, and we still need to consider updates. If an article is updated in the background, we You have to update the static pages. There are many solutions: one is to delete the corresponding static pages synchronously when the content is updated in the background. We introduce another one here, scheduled update, which means that the static page has a certain validity period and is automatically updated after this validity period. To implement this logic, we need to get the creation time of the static page in the OnActionExecuting method, and then compare it with the current time to determine whether it has expired. If it has not expired, the content will be output directly. If it has expired, continue to execute the following logic. The specific code is as follows:
//获取文件信息对象 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; } } }
At this point, pseudo-static is achieved. The current processing method can only improve access performance to a certain extent, but it may not be enough for large portal systems. According to the method introduced above, other functional extensions can be carried out. For example, after generating a static page, it can be published to a CDN, or it can be published to a separate content server, etc. Regardless of the method, the implementation idea is the same.
The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.
For more articles related to asp.net core mvc implementing pseudo-static functions, please pay attention to the PHP Chinese website!