我有一个控制器在我的MVC应用程序,我正在尝试使用自定义ActionFilterattribute记录细节,通过使用onResultExecuted方法.
I read this tutorial了解和编写我自己的动作过滤器.问题是如何将变量从控制器传递给动作过滤器?
>我想得到调用控制器的输入变量.说,用户名/用户ID.
>如果(在某些情况下)任何控制器方法抛出异常,我也想记录错误.
控制器 –
[MyActionFilter] public class myController : ApiController { public string Get(string x,int y) { .. } public string somemethod { .. } }
动作过滤器 –
public class MyActionFilterattribute : ActionFilterattribute { public override void onActionExecuted(HttpActionExecutedContext actionExecutedContext) { // HOW DO I ACCESS THE VARIABLES OF THE CONTROLLER HERE // I NEED TO LOG THE EXCEPTIONS AND THE ParaMETERS PASSED TO THE CONTROLLER METHOD } }
解决方法
方法 – 1
动作过滤器
public class MyActionFilter : ActionFilterattribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { base.OnActionExecuted(filterContext); } }
行动方法
[MyActionFilter] public ActionResult Index() { ViewBag.ControllerVariable = "12"; return View(); }
如果您注意截图,可以看到ViewBag信息
方法 – 2
动作过滤器
public class MyActionFilter : ActionFilterattribute { //Your Properties in Action Filter public string Property1 { get; set; } public string Property2 { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); } }
行动方法
[MyActionFilter(Property1 = "Value1",Property2 = "Value2")] public ActionResult Index() { return View(); }
原文地址:https://www.jb51.cc/aspnet/250561.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。