1. 程式人生 > >第三篇 基於.net搭建熱插拔式web框架(重造Controller)

第三篇 基於.net搭建熱插拔式web框架(重造Controller)

public class HuberController
    {

        public dynamic ViewBag = new DynamicViewBag();
        public ViewDataDictionary ViewData = new ViewDataDictionary();

       
        /// <summary>設定ViewBag的值
        /// </summary>
        /// <param name="key">鍵</param>
        /// <param name="value">值</param>
        internal void AddViewBageValues(string key, object value)
        {

            Impromptu.InvokeSet(ViewBag, key, value);

        }

        /// <summary>返回試圖的執行結果
        /// </summary>
        /// <returns></returns>
        protected string View()
        {
            var tKey = Engine.Razor.GetKey(getActionPath(), ResolveType.Global);
            return new CompileView().RunCompile(tKey, null, null, ViewBag);
        }
        /// <summary>返回試圖的執行結果
        /// </summary>
        /// <typeparam name="T">model的型別</typeparam>
        /// <param name="model">model</param>
        /// <returns></returns>
        protected string View<T>(T model)
        {
            var tKey = Engine.Razor.GetKey(getActionPath(), ResolveType.Global);
            return new CompileView().RunCompile(tKey, typeof(T), model, ViewBag);
        }
        /// <summary>返回試圖的執行結果
        /// </summary>
        /// <param name="viewName">檢視的全路徑(相對於執行目錄的全路徑)</param>
        /// <returns></returns>
        protected string View(string viewName)
        {
            var tKey = Engine.Razor.GetKey(getActionPathWith(viewName), ResolveType.Global);
            return new CompileView().RunCompile(tKey, null, null, ViewBag);
        }
        /// <summary>返回試圖的執行結果
        /// </summary>
        /// <typeparam name="T">model的型別</typeparam>
        /// <param name="viewName">檢視的全路徑(相對於執行目錄的全路徑)</param>
        /// <param name="model">model</param>
        /// <returns></returns>
        protected string View<T>(string viewName, T model)
        {
            var tKey = Engine.Razor.GetKey(getActionPathWith(viewName), ResolveType.Global);
            return new CompileView().RunCompile(tKey, typeof(T), model, ViewBag);
        }

        /// <summary>返回區域性試圖的執行結果
        /// </summary>
        /// <returns></returns>
        protected string PartialView()
        {
            var tKey = Engine.Razor.GetKey(getActionPath(), ResolveType.Include);
            return new CompileView().RunCompile(tKey, null, null, ViewBag);
        }
        /// <summary>返回區域性試圖的執行結果
        /// </summary>
        /// <typeparam name="T">model的型別</typeparam>
        /// <param name="model">model</param>
        /// <returns></returns>
        protected string PartialView<T>(T model)
        {
            var tKey = Engine.Razor.GetKey(getActionPath(), ResolveType.Include);
            return new CompileView().RunCompile(tKey, typeof(T), model, ViewBag);
        }
        /// <summary>返回區域性試圖的執行結果
        /// </summary>
        /// <param name="viewName">檢視的全路徑(相對於執行目錄的全路徑)</param>
        /// <returns></returns>
        protected string PartialView(string viewName)
        {
            var tKey = Engine.Razor.GetKey(getActionPathWith(viewName), ResolveType.Include);
            return new CompileView().RunCompile(tKey, null, null, ViewBag);
        }
        /// <summary>返回區域性試圖的執行結果
        /// </summary>
        /// <typeparam name="T">model的型別</typeparam>
        /// <param name="viewName">檢視的全路徑(相對於執行目錄的全路徑)</param>
        /// <param name="model">model</param>
        /// <returns></returns>
        protected string PartialView<T>(string viewName, T model)
        {
            var tKey = Engine.Razor.GetKey(getActionPathWith(viewName), ResolveType.Include);
            return new CompileView().RunCompile(tKey, typeof(T), model, ViewBag);
        }



        /// <summary>獲取action對應view的物理檔案地址
        /// </summary>
        /// <returns></returns>
        private string getActionPath()
        {
            string key = string.Empty;
            StackTrace trace = new StackTrace();
            MethodBase methodName = trace.GetFrame(2).GetMethod();
            string className = methodName.ReflectedType.FullName;

            string assName = HuberHttpModule.CurDomainAssemblyName;
            key = className.Substring(assName.Length);
            key = key.Replace(".Controllers.", ".Views.");
            key = key.Substring(0, key.Length - 10);
            key = key.Replace(".", "\\");
            key += "\\" + methodName.Name + ".cshtml";
            return key;
        }
        /// <summary>根據action名獲取其對應view的物理檔案地址
        /// </summary>
        /// <param name="ActionName">action名(同一controller中)</param>
        /// <returns></returns>
        private string getActionPathWith(string ActionName)
        {
            string key = string.Empty;
            StackTrace trace = new StackTrace();
            MethodBase methodName = trace.GetFrame(2).GetMethod();
            string className = methodName.ReflectedType.FullName;

            string assName = HuberHttpModule.CurDomainAssemblyName;
            key = className.Substring(assName.Length);
            key = key.Replace(".Controllers.", ".Views.");
            key = key.Substring(0, key.Length - 10);
            key = key.Replace(".", "\\");
            key += "\\" + ActionName + ".cshtml";
            return key;
        }
    }