首頁  >  文章  >  後端開發  >  asp.net core mvc實作偽靜態功能

asp.net core mvc實作偽靜態功能

高洛峰
高洛峰原創
2017-02-20 17:00:221401瀏覽

這篇文章主要為大家詳細介紹了asp.net core mvc實現偽靜態功能的相關資料,具有一定的參考價值,有興趣的小伙伴們可以參考一下

      在大型網站系統中,為了提高系統存取效能,往往會把一些不常變得內容發佈成靜態頁,例如商城的產品詳情頁,新聞詳情頁,這些資訊一旦發布後,變化的頻率不會很高,如果還採用動態輸出的方式處理的話,一定會對伺服器造成很大的資源浪費。但是我們又不能針對這些內容都獨立製作靜態頁,所以我們可以在系統中利用偽靜態的方式進行處理,至於什麼是偽靜態,大家可以百度下。我們這裡就來介紹一下,在asp.net core mvc中實作偽靜態的方式。

  mvc框架中,view代表的是視圖,它執行的結果就是最終輸出到客戶端瀏覽器的內容,包含html,css,js等。如果我們想要實現靜態化,我們就需要把view執行的結果保存成一個靜態文件,保存到指定的位置上,比如磁碟、分散式緩存等,下次再訪問就可以直接讀取保存的內容,而不用再執行一次業務邏輯。那asp.net core mvc要實現這樣的功能,該怎麼做?答案是使用過濾器,在mvc框架中,提供了多種過濾器類型,這裡我們要使用的是動作過濾器,動作過濾器提供了兩個時間點:動作執行前,動作執行後。我們可以在動作執行前,先判斷是否已經生成了靜態頁,如果已經生成,直接讀取文件內容輸出即可,後續的邏輯就執行跳過。如果沒有生產,就繼續往下走,在動作執行後這個階段捕捉結果,然後把結果產生的靜態內容保存下來。

  那我們就來具體的實作程式碼,首先我們定義一個過濾器類型,我們成為StaticFileHandlerFilterAttribute,這個類別派生自框架中提供的ActionFilterAttribute,StaticFileHandlerFilterAttribute重寫基底類別提供的兩個方法:OnActionExecuted(OnActionExecuted(動作執行後),OnActionExecuting(動作執行前),具體程式碼如下:

[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){}
}

在OnActionExecuting中,需要判斷下靜態內容是否已經生成,如果已經產生直接輸出內容,邏輯實作如下:

//按照一定的规则生成静态文件的名称,这里是按照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;
    }
  }
}

  在OnActionExecuted中我們需要結果動作結果,判斷動作結果類型是否是一個ViewResult,如果是透過程式碼執行這個結果,取得結果輸出,依照上面一樣的規則,產生靜態頁,具體實作如下

//获取结果
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;
    }

  上面提到的Key,我們直接增加對應的屬性

public string Key
{
  get;set;
}

  這樣我們就可以使用這個過濾器了,使用的方法:在控制器或控制器方法上增加[StaticFileHandlerFilter]特性,如果想配置不同的Key,可以使用[StaticFileHandlerFilter(Key="設定的值")]

  靜態化已經實現了,我們還需要考慮更新的事,如果後台把一篇文章更新了,我們得把靜態頁也更新下,方案有很多:一種是在後台進行內容更新時,同步把對應的靜態頁刪除即可。我們這裡介紹另一種,定時更新,就是讓靜態頁有一定的有效期限,過了這個有效期限自動更新。要實現這個邏輯,我們需要在OnActionExecuting方法中取得靜態頁的創建時間,然後跟當前時間對比,判斷是否已過期,如果未過期直接輸出內容,如果已過期,繼續執行後面的邏輯。具體程式碼如下:

//获取文件信息对象
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;
    }
  }
}

  到此偽靜態就實現好了。目前的處理方法,只能在一定程度上能夠提高存取效能,但是針對大型的門戶系統來說,可能遠遠不夠。依照上面介紹的方式,可以再進行其他功能擴展,例如生成靜態頁後可以發佈到CDN上,也可以發佈到單獨的一個內容伺服器,等等。不管是什麼方式,實現思路都是一樣的。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持PHP中文網。

更多asp.net core mvc實作偽靜態功能相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn