亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁(yè)

 更新時(shí)間:2016年05月06日 15:38:51   作者:袁剛  
這篇文章主要介紹了ASP.NET MVC使用RazorEngine解析模板生成靜態(tài)頁(yè)的相關(guān)資料,需要的朋友可以參考下

簡(jiǎn)述

      Razor是ASP.NET MVC 3中新加入的技術(shù),以作為ASPX引擎的一個(gè)新的替代項(xiàng)。在早期的MVC版本中默認(rèn)使用的是ASPX模板引擎,Razor在語(yǔ)法上的確不錯(cuò),用起來(lái)非常方便,簡(jiǎn)潔的語(yǔ)法與.NET Framework 結(jié)合,廣泛應(yīng)用于ASP.NET MVC 項(xiàng)目。

      我們?cè)诤芏囗?xiàng)目開(kāi)發(fā)中會(huì)常常用到頁(yè)面靜態(tài)化,頁(yè)面靜態(tài)化有許多方式,最常見(jiàn)的就是類似很多PHP CMS種使用的 標(biāo)簽替換的方式(如:帝國(guó)CMS、EcShop等),還有很多都是偽靜態(tài),偽靜態(tài)我們就不做過(guò)多解釋,通過(guò)路由或Url重寫來(lái)實(shí)現(xiàn)就可以了。Razor為我們提供了更加方便的模板解析方式,任何東西都是兩方面的,技術(shù)也是如此,Razor解析模板雖然更加方便、簡(jiǎn)潔,但是對(duì)于模板制作人員來(lái)說(shuō)也是有一定的技術(shù)要求,或者對(duì)于開(kāi)發(fā)一套模板制作功能來(lái)說(shuō),考慮的要更多一些。我們不再去探究這些問(wèn)題,我們更注重哪種技術(shù)更容易、更方便、更好的滿足我們項(xiàng)目的需求。

如何使用RazorEngine

       今天來(lái)簡(jiǎn)單介紹一下如何使用RazorEngine解析模板生成靜態(tài)頁(yè)面,RazorEngine它是基于微軟的Razor之上,包裝而成的一個(gè)可以獨(dú)立使用的模板引擎。也就是說(shuō),保留了Razor的模板功能,但是使得Razor脫離于Asp.net MVC,能夠在其它應(yīng)用環(huán)境下使用,項(xiàng)目地址:https://github.com/Antaris/RazorEngine

首先我們?nèi)odeplex上下兩個(gè)需要的dll http://razorengine.codeplex.com

      看到網(wǎng)上很多介紹RazorEngine的基礎(chǔ)用法的,講解的都比較詳細(xì),對(duì)于RazorEngine運(yùn)行原理很清晰,我們?cè)谶@里就不重復(fù)介紹了。寫這篇文章是對(duì)于很多新手同學(xué)來(lái)說(shuō)比較喜歡“拿來(lái)主義”,基本的用法原理都能看懂,但是如何應(yīng)用到項(xiàng)目中還是有些不是很清晰,我們只講講如何在項(xiàng)目中運(yùn)用。

本文分為兩部分:第一個(gè)部分,基本的單數(shù)據(jù)模型模板解析;第二部分,面向接口的多數(shù)據(jù)模型模板解析

第一個(gè)部分 基本的單數(shù)據(jù)模型模板解析

一、我們創(chuàng)建一個(gè)MVC項(xiàng)目,并且添加上面的兩個(gè)DLL引用,然后我們新建一個(gè)簡(jiǎn)單的文章類

public class Articles
 {
 /// <summary>
 /// 文章ID
 /// </summary>
 public int Id { get; set; }
 /// <summary>
 /// 文章標(biāo)題
 /// </summary>
 public string Title { get; set; }
 /// <summary>
 /// 文章內(nèi)容
 /// </summary>
 public string Content { get; set; }
 /// <summary>
 /// 作者
 /// </summary>
 public string Author { get; set; }
 /// <summary>
 /// 發(fā)布時(shí)間
 /// </summary>
 public DateTime CreateDate { get; set; }
 }

二、我們新建一個(gè)Razor的Html模板

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title>@Model.Title</title>
</head>
<body>
 <h1>@Model.Title</h1>
 <p>作者:@Model.Author - 發(fā)布時(shí)間:@Model.CreateDate</p>
 <p>@Raw(Model.Content)</p>
</body>
</html>

說(shuō)明:Model就是我們的文章實(shí)體類  在MVC的試圖頁(yè)cshtml中 我們一般都是在控制器里傳遞這個(gè)實(shí)體類 然后在視圖頁(yè)中 @model Models.Articles 來(lái)接收這個(gè)實(shí)體類 然后通過(guò)“@Model.”來(lái)輸出內(nèi)容,在Razor模板中是一樣的,只是不用@model Models.Articles 來(lái)接收了,其它的語(yǔ)法跟在.cshtml試圖頁(yè)中是一樣的,這么說(shuō)多余了,因?yàn)閷懛ú灰粯铀筒皇荝azor了

三、我們寫一個(gè)方法來(lái)獲取模板頁(yè)的Html代碼

 /// <summary>
 /// 獲取頁(yè)面的Html代碼
 /// </summary>
 /// <param name="url">模板頁(yè)面路徑</param>
 /// <param name="encoding">頁(yè)面編碼</param>
 /// <returns></returns>
 public 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 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; }
 }

四、我們寫一個(gè)方法 用于生成Html靜態(tài)頁(yè)

/// <summary>
 /// 創(chuàng)建靜態(tài)文件
 /// </summary>
 /// <param name="result">Html代碼</param>
 /// <param name="createpath">生成路徑</param>
 /// <returns></returns>
 public 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 { return false; }
  }
  return false;
 }

五、我們來(lái)寫個(gè)方法調(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 bool CreateStaticPage(string temppath, string path, RazorEngineTemplates.Models.Articles 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;
  }
 }

好了,大功告成,是不是很簡(jiǎn)單。

這里只是一個(gè)很簡(jiǎn)單的應(yīng)用,沒(méi)有讀取數(shù)據(jù),也沒(méi)有列表,只有一個(gè)文章數(shù)據(jù)模型,下一部分我們將介紹 多模型模板解析,因?yàn)槭嵌嗄P?所以 生成靜態(tài)頁(yè)面的時(shí)候 就不是傳遞一個(gè)具體模型實(shí)體類 我們會(huì)用到 反射,通過(guò)反射模型屬性 獲取數(shù)據(jù),有不熟悉反射的可以提前研究一下,也可以直接看下一部分的反射代碼也很簡(jiǎn)單的。

第二部分 面向接口的多數(shù)據(jù)模型模板解析

這一部分,我們介紹使用接口來(lái)解析模板,包括列表等多種模型解析,用到了Spring注入和反射還有接口等,有不熟悉的可以百度搜一下或者評(píng)論留言。

我們接著上面的示例,我們新建兩個(gè)類庫(kù) 一個(gè)是存放數(shù)據(jù)模型的 我們叫Domain;另外一個(gè)是接口和實(shí)現(xiàn)類的 我們叫Service,然后我們添加他們之間的引用

一、我們?cè)贒omain下創(chuàng)建幾個(gè)測(cè)試類

Articles - 文章測(cè)試類

Company - 公司測(cè)試類

Column - 欄目測(cè)試類

TemplateView - 模型解析類(這個(gè)是不是比較弱智?我也沒(méi)深入研究多個(gè)模型怎么反射出來(lái) 所以 我加了這么個(gè)算是公用的類 沒(méi)有對(duì)應(yīng)的數(shù)據(jù)表 只是解析模板的時(shí)候 作為中間件用用)

public class Articles
 {
 /// <summary>
 /// 文章ID
 /// </summary>
 public int Id { get; set; }
 /// <summary>
 /// 文章標(biāo)題
 /// </summary>
 public string Title { get; set; }
 /// <summary>
 /// 文章內(nèi)容
 /// </summary>
 public string Content { get; set; }
 /// <summary>
 /// 作者
 /// </summary>
 public string Author { get; set; }
 /// <summary>
 /// 發(fā)布時(shí)間
 /// </summary>
 public DateTime CreateDate { get; set; }
 }
  public class Company
 {
 /// <summary>
 /// 公司Id
 /// </summary>
 public int Id { get; set; }
 /// <summary>
 /// 公司名稱
 /// </summary>
 public string CompanyName { get; set; }
 /// <summary>
 /// 公司電話
 /// </summary>
 public string CompanyTel { get; set; }
 /// <summary>
 /// 聯(lián)系人
 /// </summary>
 public string ContectUser { get; set; }
 /// <summary>
 /// 創(chuàng)建時(shí)間
 /// </summary>
 public DateTime CreateDate { get; set; }
 }
   public class Column
 {
 /// <summary>
 /// 欄目ID
 /// </summary>
 public int Id { get; set; }
 /// <summary>
 /// 欄目名稱
 /// </summary>
 public string Title { get; set; }
 /// <summary>
 /// 文章列表
 /// </summary>

 public virtual ICollection<Articles> Articles { get; set; }
 }
   public class TemplateView
 {
 /// <summary>
 /// ID
 /// </summary>
 public int Id { get; set; }
 /// <summary>
 /// 標(biāo)題
 /// </summary>
 public string Title { get; set; }
 /// <summary>
 /// 內(nèi)容
 /// </summary>
 public string Content { get; set; }
 /// <summary>
 /// 作者
 /// </summary>
 public string Author { get; set; }
 /// <summary>
 /// 時(shí)間
 /// </summary>
 public DateTime CreateDate { get; set; } 
 /// <summary>
 /// 公司名稱
 /// </summary>
 public string CompanyName { get; set; }
 /// <summary>
 /// 公司電話
 /// </summary>
 public string CompanyTel { get; set; }
 /// <summary>
 /// 聯(lián)系人
 /// </summary>
 public string ContectUser { get; set; }
 /// <summary>
 /// 文章列表
 /// </summary>
 public virtual ICollection<Articles> Articles { get; set; }
 }

二、我們?cè)赟ervice下創(chuàng)建一個(gè)基礎(chǔ)操作接口以及其實(shí)現(xiàn)類(里面的很多方法 比如:獲取頁(yè)面的Html代碼、獲取頁(yè)面的編碼以及創(chuàng)建靜態(tài)文件等 是沒(méi)有必要寫在接口的 這個(gè)可以寫到公用的類庫(kù)里,因?yàn)檫@里就用到這么幾個(gè)方法 所以我沒(méi)有加公用類庫(kù) 就直接寫在這里面了)

/// <summary>
 /// 基礎(chǔ)操作接口
 /// </summary>
 /// <typeparam name="T"></typeparam>
 public interface IRepository<T> where T : class
 {
 /// <summary>
 /// 解析模板生成靜態(tài)頁(yè)
 /// </summary>
 /// <param name="temppath">模板地址</param>
 /// <param name="path">靜態(tài)頁(yè)地址</param>
 /// <param name="t">數(shù)據(jù)模型</param>
 /// <returns></returns>
 bool CreateStaticPage(string temppath, string path, T t); 

 /// <summary>
 /// 獲取頁(yè)面的Html代碼
 /// </summary>
 /// <param name="url">模板頁(yè)面路徑</param>
 /// <param name="encoding">頁(yè)面編碼</param>
 /// <returns></returns>
 string GetHtml(string url, System.Text.Encoding encoding);

 /// <summary>
 /// 獲取頁(yè)面的編碼
 /// </summary>
 /// <param name="html">Html源碼</param>
 /// <returns></returns>
 System.Text.Encoding GetEncoding(string html);

 /// <summary>
 /// 創(chuàng)建靜態(tài)文件
 /// </summary>
 /// <param name="result">Html代碼</param>
 /// <param name="createpath">生成路徑</param>
 /// <returns></returns>
 bool CreateFileHtmlByTemp(string result, string createpath);
 }
/// <summary>
 /// 基礎(chǔ)接口實(shí)現(xiàn)類
 /// </summary>
 /// <typeparam name="T"></typeparam>
 public abstract class RepositoryBase<T> : IRepository<T> where T : class
 {
 /// <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 bool CreateStaticPage(string temppath, string path, T t)
 {
  try
  {
  //實(shí)例化模型
  var Entity = new Domain.TemplateView();

  //獲取模板Html
  string TemplateContent = GetHtml(temppath, System.Text.Encoding.UTF8);
  //初始化結(jié)果
  string result = "";

  //反射賦值
  Type typeT = t.GetType();
  Type typeEn = Entity.GetType();

  System.Reflection.PropertyInfo[] propertyinfosT = typeT.GetProperties();

  foreach (System.Reflection.PropertyInfo propertyinfoT in propertyinfosT)
  {
   System.Reflection.PropertyInfo propertyinfoEn = typeEn.GetProperty(propertyinfoT.Name);
   if (propertyinfoEn != null && propertyinfoT.GetValue(t, null) != null)
   {
   propertyinfoEn.SetValue(Entity, propertyinfoT.GetValue(t, null), null);
   }
  }

  //很多時(shí)候 我們并沒(méi)有創(chuàng)建復(fù)雜的主外鍵關(guān)系 例如欄目下的文章 我們僅僅是在文章表中添加了一個(gè)所屬欄目ID的字段
  //并沒(méi)有創(chuàng)建關(guān)聯(lián) 這種情況下 我們直接獲取欄目的時(shí)候 是獲取不到文章列表的
  //包括很多自定義的模型和字段 比如 文章的內(nèi)容 可能不跟文章一個(gè)表 而是一個(gè)單獨(dú)的大數(shù)據(jù)字段表 這種情況下 我們的
  //TemplateView.Content就需要單獨(dú)獲取一下另一個(gè)數(shù)據(jù)模型里的 這個(gè)文章的內(nèi)容 這種時(shí)候 我們可以在這里重新給他賦值

  //如 傳入的模型是 文章
  //if(t is Domain.Articles)
  //{
  // Entity.Content= 查詢大數(shù)據(jù)字段表中這篇文章的內(nèi)容;
   
  //}

  result = Razor.Parse(TemplateContent, Entity);

  return CreateFileHtmlByTemp(result, path);
  }
  catch (Exception e)
  {
  throw e;
  }
 }

 /// <summary>
 /// 獲取頁(yè)面的Html代碼
 /// </summary>
 /// <param name="url">模板頁(yè)面路徑</param>
 /// <param name="encoding">頁(yè)面編碼</param>
 /// <returns></returns>
 public 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 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; }
 }

 /// <summary>
 /// 創(chuàng)建靜態(tài)文件
 /// </summary>
 /// <param name="result">Html代碼</param>
 /// <param name="createpath">生成路徑</param>
 /// <returns></returns>
 public 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 { return false; }
  }
  return false;
 }
 }

三、我們分別創(chuàng)建 文章管理、公司管理、欄目管理的接口和實(shí)現(xiàn)類 并且他們都集成基礎(chǔ)操作

   /// <summary>
 /// 文章管理
 /// </summary>
   public interface IArticleManage:IRepository<Domain.Articles>
 {
 }
 public class ArticleManage:RepositoryBase<Domain.Articles>,IArticleManage
 {
 }

  /// <summary>
 /// 公司管理
 /// </summary>
 public interface ICompanyManage:IRepository<Domain.Company>
 {
 }
  public class CompanyManage:RepositoryBase<Domain.Company>,ICompanyManage
 {
 }

  //欄目管理
 public interface IColumnManage:IRepository<Domain.Column>
 {
 }
  public class ColumnManage:RepositoryBase<Domain.Column>,IColumnManage
 {
 }

四、注入Xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
 <description>Spring注入Service,容器指向本層層封裝的接口</description>
 <object id="Service.ArticleManage" type="Service.ArticleManage,Service" singleton="false">
 </object>
 <object id="Service.ColumnManage" type="Service.ColumnManage,Service" singleton="false">
 </object>
 <object id="Service.CompanyManage" type="Service.CompanyManage,Service" singleton="false">
 </object>
</objects>

五、我們分別初始化一個(gè)文章類、一個(gè)公司類(沒(méi)有管理數(shù)據(jù)表,它下面沒(méi)有文章列表 欄目模型我就不初始化了,怎么輸出列表 大家可以參考下 欄目模板)

public class HomeController : Controller
 {
 /// <summary>
 /// 聲明一下注入接口
 /// </summary>
 public IArticleManage ArticleManage = Spring.Context.Support.ContextRegistry.GetContext().GetObject("Service.ArticleManage") as IArticleManage;
 public ICompanyManage CompanyManage = Spring.Context.Support.ContextRegistry.GetContext().GetObject("Service.CompanyManage") as ICompanyManage;
 public IColumnManage ColumnManage = Spring.Context.Support.ContextRegistry.GetContext().GetObject("Service.ColumnManage") as IColumnManage;


 public ActionResult Index()
 {
  //初始化一個(gè)文章數(shù)據(jù)模型
  var entityArticle = new Domain.Articles() { Id = 1, Title = "這里是文章標(biāo)題", Content = "<span style=\"color:red;\">這里是文章內(nèi)容</span>", Author = "張三", CreateDate = DateTime.Now };

  //初始化一個(gè)公司數(shù)據(jù)模型
  var entityCompany = new Domain.Company() { Id = 1, CompanyName = "這里是公司名稱", CompanyTel = "公司電話", ContectUser = "張三", CreateDate = DateTime.Now };

  //調(diào)用方法生成靜態(tài)頁(yè)面
  ArticleManage.CreateStaticPage(Server.MapPath("/Templates/Temp_article.html"), Server.MapPath("/Pages/news/" + DateTime.Now.ToString("yyyyMMddHHmmss") + "1.html"), entityArticle);
  CompanyManage.CreateStaticPage(Server.MapPath("/Templates/Temp_company.html"), Server.MapPath("/Pages/news/" + DateTime.Now.ToString("yyyyMMddHHmmss") + "2.html"), entityCompany);

  return View();
 }

 public ActionResult About()
 {
  ViewBag.Message = "Your application description page.";

  return View();
 }

 public ActionResult Contact()
 {
  ViewBag.Message = "Your contact page.";

  return View();
 }
 
 }

六、這是測(cè)試的簡(jiǎn)單的文章模板、公司模板和欄目模板

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>@Model.Title</title>
</head>
<body>
 <h1>@Model.Title</h1>
 <p>作者:@Model.Author - 發(fā)布時(shí)間:@Model.CreateDate</p>
 <p>@Raw(Model.Content)</p>
</body>
</html>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title></title>
</head>
<body>
 <p>公司名稱:@Model.CompanyName</p>
 <p>公司電話:@Model.CompanyTel</p>
 <p>聯(lián)系人:@Model.ContectUser</p>
 <p>創(chuàng)建時(shí)間:@Model.CreateDate</p>
</body>
</html>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title></title>
</head>
<body>
 <p>欄目標(biāo)題: @Model.Title</p>
 <p>
 文章列表
 <ul>
  @foreach(var item in @Model.Articles)
  {
  <li>
  <a href="">
   <span>@item.Title</span>
   <span>@item.Author</span>
   <span>@item.CreateDate</span>
  </a>
  </li>
  }
 </ul>
 </p>
</body>
</html>

我們運(yùn)行一下,大功告成~~~

        

怎么排序?怎么獲取前幾條?怎么格式化日期時(shí)間?怎么分頁(yè)?

這可是Razor啊,這都不需要再多講了吧,簡(jiǎn)單一說(shuō),如果你傳入數(shù)據(jù)前沒(méi)有事先排序或者獲取前幾條,這些操作要做模板里操作 那跟在.cshtml里基本是一樣的

@foreach(var item in @Model.ListColumn)
{

 <div >
@if (@item.LinkUrl==null)
 {
 <ul>
@foreach(var article in @item.COM_ARTICLE.Take(15).OrderByDescending(p=>p.UpDateDate))
{

<li>
  <a href="@article.LinkUrl" class="gd-a">
  <div>@article.Title</div></a>
  </li>
}
 </ul>
 }
 else
 {

 }
</div>
}

應(yīng)用還是很廣泛的,而且解析代碼相對(duì)于標(biāo)簽替換來(lái)說(shuō)十分簡(jiǎn)潔、高效。有時(shí)間可以多研究研究,改天有空寫一個(gè)模板替換標(biāo)簽的供大家參考一下。

還是那句老話,這篇文章僅僅是個(gè)人的一些理解和實(shí)現(xiàn),可能中間會(huì)出現(xiàn)一些不合理的地方或是錯(cuò)誤,請(qǐng)大家指正,我們共同學(xué)習(xí)研究。
以上就是本文的全部?jī)?nèi)容,希望大家喜歡。
原文地址:http://yuangang.cnblogs.com

相關(guān)文章

最新評(píng)論