namespace HR.Helpers
{
public class ControllerBase : Controller
{
///
/// 操作人,传IP....到后端记录
///
public virtual Operater Operater
{
get
{
return null;
}
}
///
/// 分页大小
///
public virtual int PageSize
{
get
{
return 15;
}
}
protected ContentResult JsonP(string callback, object data)
{
var json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
return this.Content(string.Format("{0}({1})", callback, json));
}
///
/// 当弹出DIV弹窗时,需要刷新浏览器整个页面
///
///
public ContentResult RefreshParent(string alert = null)
{
var script = string.Format("", string.IsNullOrEmpty(alert) ? string.Empty : "alert('" + alert + "')");
return this.Content(script);
}
public new ContentResult RefreshParentTab(string alert = null)
{
var script = string.Format("", string.IsNullOrEmpty(alert) ? string.Empty : "alert('" + alert + "')");
return this.Content(script);
}
///
/// 用JS关闭弹窗
///
///
public ContentResult CloseThickbox()
{
return this.Content("");
}
///
/// 警告并且历史返回
///
///
///
public ContentResult Back(string notice)
{
var cOntent= new StringBuilder("");
return this.Content(content.ToString());
}
public ContentResult PageReturn(string msg, string url = null)
{
var cOntent= new StringBuilder("");
return this.Content(content.ToString());
}
///
/// 转向到一个提示页面,然后自动返回指定的页面
///
///
///
///
public ContentResult Stop(string notice, string redirect, bool isAlert = false)
{
var cOntent= "" + notice + "";
if (isAlert)
cOntent= string.Format("", notice, redirect);
return this.Content(content);
}
///
/// 在方法执行前更新操作人
///
///
public virtual void UpdateOperater(ActionExecutingContext filterContext)
{
if (this.Operater == null)
return;
WCFContext.Current.Operater = this.Operater;
}
public virtual void ClearOperater()
{
//TODO
}
///
/// AOP拦截,在Action执行后
///
///
filter context
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
if (!filterContext.RequestContext.HttpContext.Request.IsAjaxRequest() && !filterContext.IsChildAction)
RenderViewData();
this.ClearOperater();
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
this.UpdateOperater(filterContext);
base.OnActionExecuting(filterContext);
//在方法执行前,附加上PageSize值
filterContext.ActionParameters.Values.Where(v => v is Request).ToList().ForEach(v => ((Request)v).PageSize = this.PageSize);
}
///
/// 产生一些视图数据
///
protected virtual void RenderViewData()
{
}
///
/// 当前Http上下文信息,用于写Log或其他作用
///
public WebExceptionContext WebExceptionContext
{
get
{
var exceptiOnContext= new WebExceptionContext
{
IP = Fetch.UserIp,
CurrentUrl = Fetch.CurrentUrl,
RefUrl = (Request == null || Request.UrlReferrer == null) ? string.Empty : Request.UrlReferrer.AbsoluteUri,
IsAjaxRequest = (Request == null) ? false : Request.IsAjaxRequest(),
FormData = (Request == null) ? null : Request.Form,
QueryData = (Request == null) ? null : Request.QueryString,
RouteData = (Request == null || Request.RequestCOntext== null || Request.RequestContext.RouteData == null) ? null : Request.RequestContext.RouteData.Values
};
return exceptionContext;
}
}
///
/// 发生异常写Log
///
///
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
var e = filterContext.Exception;
LogException(e, this.WebExceptionContext);
}
protected virtual void LogException(Exception exception, WebExceptionContext exceptiOnContext= null)
{
//do nothing!
}
}
public class WebExceptionContext
{
public string IP { get; set; }
public string CurrentUrl { get; set; }
public string RefUrl { get; set; }
public bool IsAjaxRequest { get; set; }
public NameValueCollection FormData { get; set; }
public NameValueCollection QueryData { get; set; }
public RouteValueDictionary RouteData { get; set; }
}
}
namespace HR
{
public abstract class ControllerBase:HR.Helpers.ControllerBase
{
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
}
}
}
namespace HR.Helpers
{
public class PermissionAttribute : FilterAttribute, IActionFilter
{
public List
Permissions { get; set; }
public PermissionAttribute(params EnumMoudle[] parameters)
{
PermissiOns= parameters.ToList();
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
//throw new NotImplementedException();
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
//throw new NotImplementedException();
}
}
}