【发布时间】:2013-06-03 18:39:36
【问题描述】:
如何将参数传递给我的自定义过滤器。我尝试了以下方式,但我不知道如何传递参数。
public class AuditAttribute : ActionFilterAttribute
{
private PCBAuditEntities _entity = new PCBAuditEntities();
public bool IsRequired { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//Stores the Request in an Accessible object
HttpRequestBase request = filterContext.HttpContext.Request;
string actionname = filterContext.ActionDescriptor.ActionName;
//Generate an audit
Audit audit = new Audit()
{
IpAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? request.UserHostAddress,
UrlAccessed = request.RawUrl,
TimeAccessed = DateTime.UtcNow,
UserName = (request.IsAuthenticated) ? filterContext.HttpContext.User.Identity.Name : "Anonymous",
Actionname = actionname,
EntityName ="" ,//what entity i changed?
FieldName = "",// How to find the FieldName?
Operations = "",// what operatins client did?
NewValue = "",// what is the new value?
Oldvalue = "",// what is the old value?
};
_entity.Audits.Add(audit);
_entity.SaveChanges();
base.OnActionExecuting(filterContext);
}
}
[Audit(IsRequired = true)]
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
在上面的代码IsRequired中,我们只指定了true或false,所以我很容易发送参数是否需要发送EntityName,FieldName,NewValue,Oldvalue
如何从控制器发送值?
【问题讨论】:
-
How can i send the above values from controller?- 您订阅了OnActionExecuting事件,该事件在控制器动作运行之前执行。所以谈论将这些值从控制器发送到动作过滤器是没有意义的。这就像在您实际购买汽车之前尝试开车一样。 -
@DarinDimitrov 抱歉,我不了解自定义过滤器
标签: c# asp.net-mvc asp.net-mvc-4 filter