使用NVelocity模板引擎构建ASP.NET网站
来源:互联网 发布:中南大学教务网络 编辑:程序博客网 时间:2024/05/16 12:36
public interface IHandlerFactory { /// <summary> /// 页面加载 /// </summary> /// <param name="context"></param> void Page_Load(ref VelocityContext context); /// <summary> /// 处理POST数据 /// </summary> /// <param name="context"></param> void Page_PostBack(ref VelocityContext context); } /// <summary> /// NVelocity模板引擎成员类 /// </summary> public class IHandler : IHttpHandler, IRequiresSessionState { /// <summary> /// MVC操作 /// </summary> /// <param name="context"></param> public void ProcessRequest(HttpContext context) { string rootpath = context.Request.PhysicalApplicationPath; string scriptpath = context.Request.PhysicalPath; string filepath = scriptpath.Replace(rootpath, string.Empty); string filename = filepath.ToLower().Replace("/", ".").Replace("\\", ".").Replace(".aspx", string.Empty); string template = filename.Replace(".", "/") + ".html"; //获取当前请求页面所对应的html路径 string templatepath = "template\\default"; //html页面所在的文件夹根路径 string contentType = "text/html"; string http_Accept = context.Request.ServerVariables["HTTP_ACCEPT"]; VelocityEngine engine = new VelocityEngine(); engine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); engine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, rootpath + templatepath); //设置引擎加载的文档所在的根路径 engine.Init(); VelocityContext ctx = new VelocityContext(); string root = HttpContext.Current.Request.ApplicationPath; if (root == "/") root = string.Empty; ctx.Put("webpath", root); //保存页面处理时需要的相关变量 IHandlerFactory factory = null; try { factory = (IHandlerFactory)Assembly.Load("xxxx").CreateInstance("xxxx" + filename, true); //创建当前请求页面的实例 if (factory == null) { template = "error404.html"; } else { if (context.Request.HttpMethod == "POST" && !IsReSubmit(ref context, ref ctx)) { factory.Page_PostBack(ref ctx); //调用继承了该接口的请求页面的Page_PostBack方法 } else { factory.Page_Load(ref ctx); //调用继承了该接口的请求页面的Page_Load方法 } } } #region 异常 catch (InvalidCastException ex) { //异常处理,记录错误日志 //template = "500.html"; } catch (Exception ex) { //异常出处理,记录错误日志 } #endregion //执行跳转的处理 if (ctx.Get("redirecturl") != null) { string redirecturl = ctx.Get("redirecturl").ToString(); context.Response.Redirect(redirecturl); } Template tpl = engine.GetTemplate(template); StringWriter writer = new StringWriter(); tpl.Merge(ctx, writer); context.Response.ContentType = contentType; context.Response.Write(writer.GetStringBuilder().ToString()); } /// <summary> /// /// </summary> public bool IsReusable { get { return true; } } /// <summary> /// 是否重复提交 /// </summary> /// <param name="context"></param> /// <param name="ctx"></param> /// <returns></returns> private bool IsReSubmit(ref HttpContext context, ref VelocityContext ctx) { //获取上次提交的令牌 string token = context.Request.Cookies["token"] == null ? string.Empty : context.Request.Cookies["token"].Value; //为表单签名 string newToken = Common.Input.MD5(context.Request.Form == null ? string.Empty : context.Request.Form.ToString()); //和上次的令牌比较 if (token != newToken) { context.Response.Cookies["token"].Value = newToken; return false; } ctx.Put("errors", "不允许重复提交数据"); return true; } }