PageRenderTime 59ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wojilu/Web/Mvc/ControllerCore.cs

https://bitbucket.org/kingshine/wojilu
C# | 365 lines | 143 code | 73 blank | 149 comment | 22 complexity | ee6b0dc488b82502b9667da619e167c1 MD5 | raw file
Possible License(s): MIT
  1. /*
  2. * Copyright 2010 www.wojilu.com
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Text;
  19. using System.Reflection;
  20. using System.Web;
  21. using wojilu.Reflection;
  22. using wojilu.Web.Context;
  23. using wojilu.Web.Mvc.Interface;
  24. using wojilu.Web.Mvc.Attr;
  25. using wojilu.Web.Mvc.Utils;
  26. namespace wojilu.Web.Mvc {
  27. /// <summary>
  28. /// 控制器中的一些高级方法,主要由框架调用
  29. /// </summary>
  30. public class ControllerCore {
  31. private ControllerBase controller;
  32. private MvcContext ctx;
  33. public ControllerCore( ControllerBase c ) {
  34. controller = c;
  35. ctx = c.ctx;
  36. }
  37. private Template _currentView;
  38. /// <summary>
  39. /// 设置当前模板
  40. /// </summary>
  41. /// <param name="tpl">模板对象</param>
  42. public void setCurrentView( Template tpl ) {
  43. _currentView = tpl;
  44. this.ctx.utils.setGlobalVariable( tpl );
  45. if (this.getAppLang() != null) tpl.Bind( "alang", getAppLang().getLangMap() );
  46. }
  47. /// <summary>
  48. /// 获取当前模板
  49. /// </summary>
  50. /// <returns>返回一个模板对象</returns>
  51. public Template getCurrentView() {
  52. return _currentView;
  53. }
  54. private String _actionContent;
  55. /// <summary>
  56. /// 得到当前 action 的运行结果
  57. /// </summary>
  58. /// <returns></returns>
  59. public String getActionResult() {
  60. if (strUtil.HasText( _actionContent )) return _actionContent;
  61. Template t = this.getCurrentView();
  62. return t == null ? null : t.ToString();
  63. }
  64. /// <summary>
  65. /// 设置当前 action 的运行结果
  66. /// </summary>
  67. /// <param name="content"></param>
  68. public void setActionContent( String content ) {
  69. _actionContent = content;
  70. }
  71. private Boolean _isappLangLoaded = false;
  72. private LanguageSetting _langSetting;
  73. /// <summary>
  74. /// 获取当前 app 的语言包数据
  75. /// </summary>
  76. /// <returns></returns>
  77. public LanguageSetting getAppLang() {
  78. if (_isappLangLoaded) return _langSetting;
  79. if (ctx.app != null && ctx.app.getAppType() != null) {
  80. _langSetting = wojilu.lang.getByApp( ctx.app.getAppType() );
  81. }
  82. _isappLangLoaded = true;
  83. return _langSetting;
  84. }
  85. /// <summary>
  86. /// 获取某方法的所有过滤器 ActionFilter 列表
  87. /// </summary>
  88. /// <param name="method"></param>
  89. /// <returns></returns>
  90. public List<IActionFilter> getActionFilters( MethodInfo method ) {
  91. List<IActionFilter> list = new List<IActionFilter>();
  92. object[] filters = controller.utils.getAttributes( method, typeof( IActionFilter ) );
  93. if (filters == null || filters.Length == 0) return list;
  94. foreach (Object obj in filters) list.Add( (IActionFilter)obj );
  95. list.Sort( this.Compare );
  96. return list;
  97. }
  98. /// <summary>
  99. /// 给过滤器排序
  100. /// </summary>
  101. /// <param name="x"></param>
  102. /// <param name="y"></param>
  103. /// <returns></returns>
  104. private int Compare( IActionFilter x, IActionFilter y ) {
  105. if (x.Order > y.Order) return 1;
  106. if (x.Order < y.Order) return -1;
  107. return 0;
  108. }
  109. /// <summary>
  110. /// 合并需要隐藏的 LayoutController
  111. /// </summary>
  112. /// <param name="hidedCcontroller"></param>
  113. internal void addHidedLayouts( ControllerBase hidedCcontroller ) {
  114. controller.hidedLayouts.AddRange( hidedCcontroller.hidedLayouts );
  115. }
  116. internal Boolean isHided( Type layoutType ) {
  117. return controller.hidedLayouts.Contains( layoutType );
  118. }
  119. internal Boolean isCheckPermission( Type layoutType ) {
  120. return !controller.hidedPermission.Contains( layoutType );
  121. }
  122. private Type _appType;
  123. /// <summary>
  124. /// 设置当前的 app 类型
  125. /// </summary>
  126. /// <param name="t"></param>
  127. public void setAppType( Type t ) { _appType = t; }
  128. /// <summary>
  129. /// 获取当前的 app 类型
  130. /// </summary>
  131. /// <returns></returns>
  132. public Type getAppType() { return _appType; }
  133. /// <summary>
  134. /// 将页面元信息(包括Title/Keywords/Description/RssLink)赋予模板
  135. /// </summary>
  136. public void renderPageMetaToView() {
  137. if (strUtil.IsNullOrEmpty( controller.Page.Title )) controller.Page.Title = config.Instance.Site.PageDefaultTitle;
  138. if (strUtil.IsNullOrEmpty( controller.Page.Keywords )) controller.Page.Keywords = config.Instance.Site.Keywords;
  139. if (strUtil.IsNullOrEmpty( controller.Page.Description )) controller.Page.Description = config.Instance.Site.Description;
  140. String lnkRss = controller.Page.RssLink;
  141. if (strUtil.HasText( lnkRss )) {
  142. if (lnkRss.StartsWith( "<link title=" ) == false) {
  143. lnkRss = String.Format( "<link title=\"RSS\" type=\"application/rss+xml\" rel=\"alternate\" href=\"{0}\" />", lnkRss );
  144. }
  145. controller.Page.RssLink = lnkRss;
  146. }
  147. }
  148. internal void switchViewToLayout() {
  149. setCurrentView( getTemplateByAction( "Layout" ) );
  150. }
  151. /// <summary>
  152. /// 根据 action 名称获取模板对象
  153. /// </summary>
  154. /// <param name="action"></param>
  155. /// <returns></returns>
  156. public Template getTemplateByAction( String action ) {
  157. return new Template( getTemplatePathByAction( action ) );
  158. }
  159. private String getControllerDir() {
  160. String pathRaw = strUtil.GetTypeFullName( controller.GetType() );
  161. // 去掉根目录
  162. String result = trimRootNamespace( pathRaw ).TrimStart( '.' );
  163. // 换成路径分隔符
  164. result = result.Replace( '.', '/' );
  165. String pathRoot = MvcConfig.Instance.ViewDir;
  166. result = strUtil.Join( pathRoot, result );
  167. result = strUtil.TrimEnd( result, "Controller" );
  168. return result;
  169. }
  170. private String trimRootNamespace( String pathRaw ) {
  171. foreach (String ns in MvcConfig.Instance.RootNamespace) {
  172. if (pathRaw.StartsWith( ns )) return strUtil.TrimStart( pathRaw, ns );
  173. }
  174. return pathRaw;
  175. }
  176. /// <summary>
  177. /// 根据文件名称获取模板对象,文件名必须从视图 view 的根目录算起
  178. /// </summary>
  179. /// <param name="fileName"></param>
  180. /// <returns></returns>
  181. public Template getTemplateByFileName( String fileName ) {
  182. return new Template( getTemplatePathByFile( fileName ) );
  183. }
  184. /// <summary>
  185. /// 获取某 action 的模板文件的绝对路径(包括模板的后缀名)
  186. /// </summary>
  187. /// <param name="action"></param>
  188. /// <returns></returns>
  189. public String getTemplatePathByAction( String action ) {
  190. return PathHelper.Map( getControllerDir() + "/" + action + MvcConfig.Instance.ViewExt );
  191. }
  192. /// <summary>
  193. /// 获取某模板文件的绝对路径,文件名必须从视图 view 的根目录算起
  194. /// </summary>
  195. /// <param name="fileName"></param>
  196. /// <returns></returns>
  197. public String getTemplatePathByFile( String fileName ) {
  198. return PathHelper.Map( strUtil.Join( MvcConfig.Instance.ViewDir, fileName ) + MvcConfig.Instance.ViewExt );
  199. }
  200. //------------------------------------------------------------------------------------------
  201. /// <summary>
  202. /// 运行某 action
  203. /// </summary>
  204. public void runAction() {
  205. runAction( ctx.route.action );
  206. }
  207. /// <summary>
  208. /// 运行某 action
  209. /// </summary>
  210. /// <param name="actionName"></param>
  211. public void runAction( String actionName ) {
  212. ControllerRunner.runAction( controller, actionName );
  213. //MethodInfo method = getMethod( actionName );
  214. //if (method == null) {
  215. // throw new Exception( "action " + wojilu.lang.get( "exNotFound" ) );
  216. //}
  217. //ParameterInfo[] parameters = getParameters( method );
  218. //if (parameters.Length == 1) {
  219. // if (parameters[0].ParameterType == typeof( String )) {
  220. // method.Invoke( controller, new object[] { HttpUtility.UrlDecode( ctx.route.query ) } );
  221. // }
  222. // else {
  223. // method.Invoke( controller, new object[] { ctx.route.id } );
  224. // }
  225. //}
  226. //else if (parameters.Length == 0) {
  227. // method.Invoke( controller, null );
  228. //}
  229. //else {
  230. // throw new Exception( "action " + wojilu.lang.get( "exNotFound" ) );
  231. //}
  232. }
  233. /// <summary>
  234. /// 根据名称获取某 action 的方法信息
  235. /// </summary>
  236. /// <param name="actionName"></param>
  237. /// <returns></returns>
  238. public MethodInfo getMethod( String actionName ) {
  239. return controller.GetType().GetMethod( actionName );
  240. }
  241. /// <summary>
  242. /// 根据批注类型,获取某方法的特定批注
  243. /// </summary>
  244. /// <param name="method"></param>
  245. /// <param name="attrType"></param>
  246. /// <returns></returns>
  247. public Attribute getAttribute( MethodInfo method, Type attrType ) {
  248. return ReflectionUtil.GetAttribute( method, attrType );
  249. }
  250. /// <summary>
  251. /// 获取某方法的所有批注
  252. /// </summary>
  253. /// <param name="method"></param>
  254. /// <returns></returns>
  255. public object[] getAttributesAll( MethodInfo method ) {
  256. return ReflectionUtil.GetAttributes( method );
  257. }
  258. /// <summary>
  259. /// 根据批注类型,获取某方法的特定批注列表
  260. /// </summary>
  261. /// <param name="method"></param>
  262. /// <param name="attrType"></param>
  263. /// <returns></returns>
  264. public object[] getAttributes( MethodInfo method, Type attrType ) {
  265. return ReflectionUtil.GetAttributes( method, attrType );
  266. }
  267. /// <summary>
  268. /// 获取某方法的所有 HttpMethod 类型的批注
  269. /// </summary>
  270. /// <param name="method"></param>
  271. /// <returns></returns>
  272. public object[] getHttpMethodAttributes( MethodInfo method ) {
  273. return this.getAttributes( method, typeof( IHttpMethod ) );
  274. }
  275. /// <summary>
  276. /// 获取某方法的所有参数信息
  277. /// </summary>
  278. /// <param name="method"></param>
  279. /// <returns></returns>
  280. //public ParameterInfo[] getParameters( MethodInfo method ) {
  281. // return method.GetParameters();
  282. //}
  283. //------------------------------------------------------------------------------------------
  284. private Boolean _isrunAction = true;
  285. internal Boolean IsRunAction() {
  286. return _isrunAction;
  287. }
  288. internal void IsRunAction( Boolean run ) {
  289. _isrunAction = run;
  290. }
  291. }
  292. }