第四篇 基於.net搭建熱插拔式web框架(RazorEngine實現)
阿新 • • 發佈:2018-12-30
/// <summary>頁面幫助類 /// A simple helper demonstrating the @Html.Raw /// </summary> /// <typeparam name="T"></typeparam> public class HuberImplementingTemplateBase<T> : TemplateBase<T> { /// <summary> /// A simple helper demonstrating the @Html.Raw /// </summary> public HuberImplementingTemplateBase() { Html = new RazorHtmlHelper(); } /// <summary> /// A simple helper demonstrating the @Html.Raw /// /// </summary> public RazorHtmlHelper Html { get; set; } } public class RazorHtmlHelper { /// <summary> /// 呼叫Action檢視 /// </summary> /// <param name="actionName">action方法名稱</param> /// <param name="controllerName">控制器名稱</param> /// <returns></returns> public IEncodedString Action(string actionName, string controllerName) { return Action(actionName, controllerName, new { }); } /// <summary> /// 呼叫Action檢視 /// </summary> /// <param name="actionName"></param> /// <param name="controllerName"></param> /// <param name="routeValues">傳入引數</param> /// <returns></returns> public IEncodedString Action(string actionName, string controllerName, object routeValues) { RefRequestEntity paras = SetParamValue(routeValues); var t = HuberHttpModule.CurDomainAssembly.GetType(HuberHttpModule.CurDomainAssemblyName + ".Controllers." + controllerName + "Controller"); var m = t.GetMethod(actionName); object dObj = Activator.CreateInstance(t); object result = m.Invoke(dObj, new object[] { paras }); return new RawString((result as RefRespondEntity).ResultContext.ToString()); } /// <summary> /// 根據model設定傳入引數 /// </summary> /// <param name="routeValues"></param> /// <returns></returns> private static RefRequestEntity SetParamValue(object routeValues) { RefRequestEntity paras = new RefRequestEntity(); Type t1 = routeValues.GetType(); PropertyInfo[] pis = t1.GetProperties(); foreach (PropertyInfo pi in pis) { paras.Request.Add(pi.Name, pi.GetValue(routeValues)); } return paras; } public IEncodedString RenderAction(string actionName, string controllerName) { return Action(actionName, controllerName, new { }); } public IEncodedString RenderAction(string actionName, string controllerName, object routeValues) { return Action(actionName, controllerName, routeValues); } public IEncodedString RenderPartial(string partialViewName, string controllerName) { return RenderPartial(partialViewName, controllerName, new { }, new DynamicViewBag()); } // Renders the partial view with the given view data and, implicitly, the given view data's model public IEncodedString RenderPartial(string partialViewName, string controllerName, DynamicViewBag ViewBag) { return RenderPartial(partialViewName, controllerName, new { }, ViewBag); } // Renders the partial view with an empty view data and the given model public IEncodedString RenderPartial(string partialViewName, string controllerName, object model) { return RenderPartial(partialViewName, controllerName, model, new DynamicViewBag()); } // Renders the partial view with a copy of the given view data plus the given model /// <summary> /// 部分檢視 /// </summary> /// <param name="partialViewName">部分檢視名稱</param> /// <param name="controllerName">控制器名稱</param> /// <param name="model"> model</param> /// <param name="ViewBag">ViewBag</param> /// <returns></returns> public IEncodedString RenderPartial(string partialViewName, string controllerName, object model, DynamicViewBag ViewBag) { RefRequestEntity paras = SetParamValue(model); var t = HuberHttpModule.CurDomainAssembly.GetType(HuberHttpModule.CurDomainAssemblyName + ".Controllers." + controllerName + "Controller"); var ActionFunc = t.GetMethod(partialViewName); object dObj = Activator.CreateInstance(t); var AddViewBageFunc = t.GetMethod("AddViewBageValues"); foreach (string key in ViewBag.GetDynamicMemberNames()) { AddViewBageFunc.Invoke(dObj, new object[] { key, Impromptu.InvokeGet(ViewBag, key) }); } object result = ActionFunc.Invoke(dObj, new object[] { paras }); return new RawString((result as RefRespondEntity).ResultContext.ToString()); } }