现在的位置: 主页 > 公司荣誉 > 文章列表

Asp.net动态页面静态化之输出对象属性(实体关联)

作者:潜江市宏光畜牧有限公司 来源:www.qjhgnm.com 未知发布时间:2017-09-07 13:25:01
Asp.net动态页面静态化之输出对象属性(实体关联) Asp.net动态页面静态化之输出对象属性(实体关联)

模板引擎不单单可以输出单一的数据,还能直接输出对象的属性

using System; using System.Collections.Generic; using System.Linq; using System.Web; using NVelocity.Runtime; using NVelocity; using NVelocity.App; namespace czbk { /// /// Handler2 的摘要说明 /// public class Handler2 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = text/html;//plain表示普通文本格式输出 静态化应该输出html格式文本 User user = new User(); user.Username = 王大锤; user.Userphone = 18312345678; Role role = new Role(); role.Roleid = 1001; role.Rolename = 超级搞笑王; user.User_Role1 = role;//给关联对象赋值 VelocityEngine vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, file); vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath(~/));//模板文件所在的文件夹 vltEngine.Init(); VelocityContext vltContext = new VelocityContext(); vltContext.Put(u, user);//设置参数,在模板中可以通过$u来引用 Template vltTemplate = vltEngine.GetTemplate(Users.htm); System.IO.StringWriter vltWriter = new System.IO.StringWriter(); vltTemplate.Merge(vltContext, vltWriter); string html = vltWriter.GetStringBuilder().ToString(); context.Response.Write(html); } public bool IsReusable { get { return false; } } } public class User { private string username; public string Username { get { return username; } set { username = value; } } private string userphone; public string Userphone { get { return userphone; } set { userphone = value; } } private Role User_Role;//引入一个关联的类对象 public Role User_Role1 { get { return User_Role; } set { User_Role = value; } } } public class Role { private string roleid; public string Roleid { get { return roleid; } set { roleid = value; } } private string rolename; public string Rolename { get { return rolename; } set { rolename = value; } } } }

前台代码

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉SEO http://wuhan.4567w.com

上一篇:Android中ActionBar居中 下一篇:最后一页