详解ASP.NET MVC 解析模板生成静态页(RazorEngine)(4)

   /// <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 type="Service.ArticleManage,Service" singleton="false"> </object> <object type="Service.ColumnManage,Service" singleton="false"> </object> <object type="Service.CompanyManage,Service" singleton="false"> </object> </objects>

五、我们分别初始化一个文章类、一个公司类(没有管理数据表,它下面没有文章列表 栏目模型我就不初始化了,怎么输出列表 大家可以参考下 栏目模板) 

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() { //初始化一个文章数据模型 var entityArticle = new Domain.Articles() { Id = 1, Title = "这里是文章标题", Content = "<span style=\"color:red;\">这里是文章内容</span>", Author = "张三", CreateDate = DateTime.Now }; //初始化一个公司数据模型 var entityCompany = new Domain.Company() { Id = 1, CompanyName = "这里是公司名称", CompanyTel = "公司电话", ContectUser = "张三", CreateDate = DateTime.Now }; //调用方法生成静态页面 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(); } }

六、这是测试的简单的文章模板、公司模板和栏目模板 

<!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 - 发布时间:@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>联系人:@Model.ContectUser</p> <p>创建时间:@Model.CreateDate</p> </body> </html>  

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdpfdp.html