ASP.NET MVC生成靜態(tài)頁(yè)面的方法
本文主要講述了在asp.NET mvc中,頁(yè)面靜態(tài)化的方法。對(duì)于網(wǎng)站來(lái)說(shuō),生成純html靜態(tài)頁(yè)面除了有利于seo外,還可以減輕網(wǎng)站的負(fù)載能力和提高網(wǎng)站性能。
1.先付上封裝好生成靜態(tài)頁(yè)的原代碼:
public class Common
{
#region 獲取模板頁(yè)的Html代碼
/// <summary>
/// 獲取頁(yè)面的Html代碼
/// </summary>
/// <param name="url">模板頁(yè)面路徑</param>
/// <param name="encoding">頁(yè)面編碼</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>
/// 獲取頁(yè)面的編碼
/// </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)頁(yè)
/// <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)頁(yè)
/// <summary>
/// 解析模板生成靜態(tài)頁(yè)
/// </summary>
/// <param name="temppath">模板地址</param>
/// <param name="path">靜態(tài)頁(yè)地址</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)頁(yè)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)頁(yè)面
/// </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)頁(yè)Html代碼
result = Razor.Parse(TemplateContent, view_Home);
//創(chuàng)建靜態(tài)文件
Common.CreateFileHtmlByTemp(result, Server.MapPath("/Resource/Manage/Html/Home/Index.html"));
}
}
4.如首頁(yè)執(zhí)行時(shí),可以在執(zhí)行Action前去執(zhí)行一個(gè)過(guò)濾器:
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í)行首頁(yè):
[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;
}
}
說(shuō)明:可以讓一個(gè)超鏈接或跳轉(zhuǎn)地址直接跳轉(zhuǎn)到一個(gè)html的靜態(tài)頁(yè)面,速度會(huì)更快;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET MVC3關(guān)于生成純靜態(tài)后如何不再走路由直接訪問(wèn)靜態(tài)頁(yè)面
- 使用ASP.NET模板生成HTML靜態(tài)頁(yè)面的五種方案
- ASP.NET動(dòng)態(tài)生成靜態(tài)頁(yè)面的實(shí)例代碼
- ASP.NET 生成靜態(tài)頁(yè)面 實(shí)現(xiàn)思路
- Asp.NET 生成靜態(tài)頁(yè)面并分頁(yè)的代碼
- Asp.Net生成靜態(tài)頁(yè)面的實(shí)現(xiàn)方法
- asp.net生成Excel并導(dǎo)出下載五種實(shí)現(xiàn)方法
- asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼
- ASP.net(c#)生成條形碼 code39條碼生成方法
- asp.net C#生成和解析二維碼的實(shí)例代碼
- Asp.net生成Excel文件并下載(更新:解決使用迅雷下載頁(yè)面而不是文件的問(wèn)題)
- ASP.NET編程簡(jiǎn)單實(shí)現(xiàn)生成靜態(tài)頁(yè)面的方法【附demo源碼下載】
相關(guān)文章
基于SignalR的消息推送與二維碼掃描登錄實(shí)現(xiàn)代碼
這篇文章主要介紹了基于SignalR的消息推送與二維碼掃描登錄實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-02-02
理解ASP.NET Core 中間件(Middleware)
這篇文章主要介紹了ASP.NET Core 中間件(Middleware),中間件是一種裝配到應(yīng)用管道以處理請(qǐng)求和響應(yīng)的軟件。文中講解相關(guān)知識(shí)非常詳細(xì),感興趣的朋友可以一起來(lái)看一看2021-09-09
ASP.NET―001:GridView綁定List、頁(yè)面返回值具體實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET―GridView綁定List、頁(yè)面返回值具體實(shí)現(xiàn),需要的朋友可以參考下2014-02-02
ASP.NET?MVC使用Knockout獲取數(shù)組元素索引的2種方法
這篇文章介紹了ASP.NET?MVC使用Knockout獲取數(shù)組元素索引的2種方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
ASP.NET?Core項(xiàng)目中調(diào)用WebService的方法
這篇文章介紹了ASP.NET?Core項(xiàng)目中調(diào)用WebService的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
ajaxToolkit:CalendarExtender演示與實(shí)現(xiàn)代碼
Ajax的CalenderExtender的日期選擇實(shí)現(xiàn)介紹,感興趣的你可以了解下,就當(dāng)是鞏固知識(shí),希望本文對(duì)你有所幫助2013-01-01
Global.asax的Application_BeginRequest實(shí)現(xiàn)url重寫無(wú)后綴的代碼
本文為大家詳細(xì)介紹下利用Global.asax的Application_BeginRequest 實(shí)現(xiàn)url重寫其無(wú)后綴,具體核心代碼如下,有需求的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08
Asp.net動(dòng)態(tài)生成html頁(yè)面的方法分享
這篇文章介紹了Asp.net動(dòng)態(tài)生成html頁(yè)面的方法,有需要的朋友可以參考一下2013-10-10

