欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

ASP.NETMVC如何生成靜態(tài)頁面-創(chuàng)新互聯(lián)

這篇文章主要介紹ASP.NET MVC如何生成靜態(tài)頁面,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)為企業(yè)級(jí)客戶提高一站式互聯(lián)網(wǎng)+設(shè)計(jì)服務(wù),主要包括網(wǎng)站制作、成都網(wǎng)站制作、App定制開發(fā)小程序開發(fā)、宣傳片制作、LOGO設(shè)計(jì)等,幫助客戶快速提升營(yíng)銷能力和企業(yè)形象,創(chuàng)新互聯(lián)各部門都有經(jīng)驗(yàn)豐富的經(jīng)驗(yàn),可以確保每一個(gè)作品的質(zhì)量和創(chuàng)作周期,同時(shí)每年都有很多新員工加入,為我們帶來大量新的創(chuàng)意。 

1.先付上封裝好生成靜態(tài)頁的原代碼:

public class Common
{
  #region 獲取模板頁的Html代碼
  /// <summary>
  /// 獲取頁面的Html代碼
  /// </summary>
  /// <param name="url">模板頁面路徑</param>
  /// <param name="encoding">頁面編碼</param>
  /// <returns></returns>
  public static string GetHtml(string url, System.Text.Encoding encoding)
  {
    byte[] buf = new WebClient().DownloadData(url);
    if (encoding != null)
    {
      return encoding.GetString(buf);
    }
    string html = System.Text.Encoding.UTF8.GetString(buf);
    encoding = GetEncoding(html);
    if (encoding == null || encoding == System.Text.Encoding.UTF8)
    {
      return html;
    }
    return encoding.GetString(buf);
  }

  /// <summary>
  /// 獲取頁面的編碼
  /// </summary>
  /// <param name="html">Html源碼</param>
  /// <returns></returns>
  public static System.Text.Encoding GetEncoding(string html)
  {
    string pattern = @"(?i)\bcharset=(?<charset>[-a-zA-Z_0-9]+)";
    string charset = Regex.Match(html, pattern).Groups["charset"].Value;
    try
    {
      return System.Text.Encoding.GetEncoding(charset);
    }
    catch (ArgumentException)
    {
      return null;
    }
  }
  #endregion

  #region 用于生成Html靜態(tài)頁
  /// <summary>
  /// 創(chuàng)建靜態(tài)文件
  /// </summary>
  /// <param name="result">Html代碼</param>
  /// <param name="createpath">生成路徑</param>
  /// <returns></returns>
  public static bool CreateFileHtmlByTemp(string result, string createpath)
  {
    if (!string.IsNullOrEmpty(result))
    {
      if (string.IsNullOrEmpty(createpath))
      {
        createpath = "/default.html";
      }
      string filepath = createpath.Substring(createpath.LastIndexOf(@"\"));
      createpath = createpath.Substring(0, createpath.LastIndexOf(@"\"));
      if (!Directory.Exists(createpath))
      {
        Directory.CreateDirectory(createpath);
      }
      createpath = createpath + filepath;

      try
      {
        FileStream fs2 = new FileStream(createpath, FileMode.Create);
        StreamWriter sw = new StreamWriter(fs2, new System.Text.UTF8Encoding(false));//去除UTF-8 BOM
        sw.Write(result);
        sw.Close();
        fs2.Close();
        fs2.Dispose();
        return true;
      }
      catch (Exception ex)
      {
        throw ex;
      }
    }
    return false;
  }
  #endregion

  #region 調(diào)用靜態(tài)模板,并且傳遞數(shù)據(jù)模型實(shí)體類 創(chuàng)建Html靜態(tài)頁
  /// <summary>
  /// 解析模板生成靜態(tài)頁
  /// </summary>
  /// <param name="temppath">模板地址</param>
  /// <param name="path">靜態(tài)頁地址</param>
  /// <param name="t">數(shù)據(jù)模型</param>
  /// <returns></returns>
  public static bool CreateStaticPage<T>(string temppath, string path, T t)
  {
    try
    {
      //獲取模板Html
      string TemplateContent = GetHtml(temppath, System.Text.Encoding.UTF8);

      //初始化結(jié)果
      string result = string.Empty;

      //解析模板生成靜態(tài)頁Html代碼
      result = Razor.Parse(TemplateContent, t);

      //創(chuàng)建靜態(tài)文件
      return CreateFileHtmlByTemp(result, path);
    }
    catch (Exception e)
    {
      throw e;
    }
  }
  #endregion
}

2.調(diào)用方法(創(chuàng)建一個(gè)多線程去執(zhí)行,效果會(huì)更好):

//實(shí)例化調(diào)用方法
Task tk = new Task(CreateStaticHtml);
tk.Start();
//靜態(tài)調(diào)用方法
Task.Factory.StartNew(() => CreateStaticHtml());

3.封裝好的靜態(tài)方法:

/// <summary>
/// 創(chuàng)建靜態(tài)頁面
/// </summary>
public void CreateStaticHtml()
{
  using (BangLiEntities bangLi = new BangLiEntities())
  {
    View_Home view_Home = new View_Home();
    view_Home.CommandAdExtendList = Dal.CommandAdDal.Instance(bangLi).GetInit().ToList();
    view_Home.NewsList = bangLi.News.OrderByDescending(u => u.AddDateTime).Take(5).ToList();
    view_Home.NewsExtendList = Dal.NewsDal.Instance(bangLi).GetInit().OrderByDescending(u => u.AddDateTime).Take(5).ToList();
    view_Home.News = Dal.NewsDal.Instance(bangLi).GetInit().Where(u => u.Id == 3).SingleOrDefault();
    string TemplateContent = Common.GetHtml(Server.MapPath("/Views/SourceHtml/Home/Index.cshtml"), System.Text.Encoding.UTF8);
    //初始化結(jié)果
    string result = string.Empty;
    //解析模板生成靜態(tài)頁Html代碼
    result = Razor.Parse(TemplateContent, view_Home);
    //創(chuàng)建靜態(tài)文件
    Common.CreateFileHtmlByTemp(result, Server.MapPath("/Resource/Manage/Html/Home/Index.html"));
  }
}

4.如首頁執(zhí)行時(shí),可以在執(zhí)行Action前去執(zhí)行一個(gè)過濾器:

public class MyFirstHomeAttribute:ActionFilterAttribute
{
  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    var context = filterContext.HttpContext;
    context.Session["IsStaticHtml"] = false;
    string path = context.Server.MapPath("/Resource/Manage/Html/Home/Index.html");
    if (System.IO.File.Exists(path))
    {
      string html = System.IO.File.ReadAllText(path);
      context.Response.Write(html);
      context.Session["IsStaticHtml"] = true;
      context.Response.End();
    }
  }
}

5.執(zhí)行首頁:

[MyFirstHome]
public ActionResult Index()
{
  View_Home view_Home = null;
  var IsStaticHtml = Convert.ToBoolean(Session["IsStaticHtml"]);
  if (!IsStaticHtml)
  {
    view_Home = new View_Home();
    using (BangLiEntities bangLi = new BangLiEntities())
    {
      view_Home.CommandAdExtendList = Dal.CommandAdDal.Instance(bangLi).GetInit().ToList();
      view_Home.NewsExtendList = Dal.NewsDal.Instance(bangLi).GetInit().OrderByDescending(u => u.AddDateTime).Take(5).ToList();
          
    }
    return View(view_Home);
  }
  else
  {
    return null;
  }
}

說明:可以讓一個(gè)超鏈接或跳轉(zhuǎn)地址直接跳轉(zhuǎn)到一個(gè)html的靜態(tài)頁面,速度會(huì)更快;


以上是“ASP.NET MVC如何生成靜態(tài)頁面”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享文章:ASP.NETMVC如何生成靜態(tài)頁面-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://chinadenli.net/article36/djgopg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、自適應(yīng)網(wǎng)站、虛擬主機(jī)、網(wǎng)站導(dǎo)航、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)
九九九热视频最新在线| 搡老妇女老熟女一区二区| 中国黄色色片色哟哟哟哟哟哟| 伊人久久五月天综合网| 日韩成人动画在线观看| 精品国产丝袜一区二区| 黄色在线免费高清观看| 一区二区欧美另类稀缺| 中文字幕亚洲在线一区| 99久久精品一区二区国产| 婷婷基地五月激情五月| 国产精品一区二区不卡中文| 91亚洲精品亚洲国产| 欧美国产亚洲一区二区三区| 国产亚洲欧美一区二区| 欧美日韩在线观看自拍| 久热青青草视频在线观看| 国产内射一级一片内射高清| 亚洲妇女作爱一区二区三区| 在线亚洲成人中文字幕高清| 国产又大又硬又粗又湿| 国产精品欧美一区两区| 神马午夜福利免费视频| 日韩中文无线码在线视频| 亚洲av熟女一区二区三区蜜桃| 国产欧美日韩在线精品一二区| 日韩精品日韩激情日韩综合| 亚洲国产综合久久天堂| 91一区国产中文字幕| 国产内射一级一片内射高清| 久久精品国产在热久久| 亚洲中文字幕高清视频在线观看| 一区二区三区国产日韩| 国产日产欧美精品大秀| 尹人大香蕉一级片免费看| 在线观看视频成人午夜| 亚洲欧美日韩综合在线成成| 精品日韩av一区二区三区| 最新国产欧美精品91| 亚洲一区二区三区有码| 白丝美女被插入视频在线观看|