public static void HandleException(System.Web.Mvc.ControllerBase controller, System.Web.HttpContextBase context, string culture, Exception exception) { try { lock (instLock) { if (handling) { return; } handling = true; } //System.Web.Security.MembershipUser user = System.Web.Security.Membership.GetUser(); DomainModel.Errors.Logger.LogException( null, culture, context.Request.UserHostAddress, context.Request.Url.AbsoluteUri, exception ); } catch (Exception ex) { // No more error handling System.Diagnostics.Debug.WriteLine(string.Format("Exception:{0}", ex)); } lock (instLock) { handling = false; } }
public static void PreventResubmit(this System.Web.Mvc.ControllerBase controller, params NoResubmitAbstract[] vModels) { var preventResubmitGuid = Guid.NewGuid(); controller.TempData["PreventResubmit"] = preventResubmitGuid; foreach (var vm in vModels) { vm.SetPreventResubmit(preventResubmitGuid); } }
public static ServiceContext GetServiceContext(this System.Web.Mvc.ControllerBase controllerBase) { var controller = controllerBase as BaseController; if (controller == null) { throw new Exception("要获取服务上下文属性,Controller必须继承BaseController基类"); } return(controller._ServiceContext); }
public static PageInfoManager Open(System.Web.Mvc.ControllerBase origin) { PageInfoManager retour = null; if (retour == null && origin.TempData["PageInfo"] != null) { retour = (PageInfoManager)origin.TempData["PageInfo"]; } else { retour = new COMPONENTS.WEB.MVC.PageInfoManager(); origin.TempData["PageInfo"] = retour; } retour.ActionName = origin.ControllerContext.RouteData.Values["action"].ToString(); retour.controllerName = origin.ControllerContext.RouteData.Values["controller"].ToString(); return(retour); }
/// <summary> /// Returns the html string /// </summary> /// <typeparam name="T">Type of the object</typeparam> /// <param name="controller">Controller object (Ex: new HomeController())</param> /// <param name="viewPath">razor view location (Ex: ~/Views/Home/Home.cshtml)</param> /// <param name="controllerName">Controller Name (Ex: HomeController)</param> /// <param name="viewName">View/Action Name (Ex: Index) </param> /// <param name="model">object</param> /// <returns>returns string</returns> public string GenerateViewToString <T>(System.Web.Mvc.ControllerBase controller, string viewPath, string controllerName, string viewName, T model) { HttpContextBase contextBase = new HttpContextWrapper(HttpContext.Current); System.Web.Mvc.ViewDataDictionary _viewDataDictionary = new System.Web.Mvc.ViewDataDictionary(model); System.Web.Mvc.TempDataDictionary _tempDataDictionary = new System.Web.Mvc.TempDataDictionary(); RouteData _routeData = new RouteData(); _routeData.Values.Add("controller", controllerName); _routeData.Values.Add("action", viewName); var controllerContext = new System.Web.Mvc.ControllerContext(contextBase, _routeData, controller); var razorViewEngine = new System.Web.Mvc.RazorViewEngine(); var razorViewResult = razorViewEngine.FindView(controllerContext, viewPath, "", false); var writer = new StringWriter(); var viewContext = new System.Web.Mvc.ViewContext(controllerContext, razorViewResult.View, _viewDataDictionary, _tempDataDictionary, writer); razorViewResult.View.Render(viewContext, writer); return(writer.ToString()); }
public static bool IsResubmit(this System.Web.Mvc.ControllerBase controller, NoResubmitAbstract vModel) { return((Guid)controller.TempData["PreventResubmit"] != vModel.PreventResubmit); }
/// <summary> /// 获取属性的查询值并处理 Controller.ModelState /// </summary> private object GetValueAndHandleModelState(PropertyInfo property, IValueProvider valueProvider, System.Web.Mvc.ControllerBase controller) { var result = valueProvider.GetValue(property.Name); if (result == null) { return(null); } var modelState = new ModelState { Value = result }; controller.ViewData.ModelState.Add(property.Name, modelState); object value = null; try { value = result.ConvertTo(property.PropertyType); } catch (Exception ex) { modelState.Errors.Add(ex); } return(value); }
/// <summary> /// Get current routing value /// </summary> /// <param name="controller"></param> /// <param name="type">Requested routing type</param> /// <returns>string</returns> public static string Routing(this System.Web.Mvc.ControllerBase controller, RoutingType type) { return(Routing(controller.ControllerContext.RouteData, type)); }