c# – MVC动作过滤器使用参数传递给ActionResult?
发布时间:2020-12-15 06:14:53 所属栏目:百科 来源:网络整理
导读:我创建了一个自定义的 Action Filter没有问题. 但是我想修改Action Filter来使用实际传递给我的方法的一些参数. 所以如果我有以下方法: [HttpPost][MyAttribute]public ActionResult ViewUserDetails(Guid userId){ // Do something} 如何从MyAttribute中访
我创建了一个自定义的
Action Filter没有问题.
但是我想修改Action Filter来使用实际传递给我的方法的一些参数. 所以如果我有以下方法: [HttpPost] [MyAttribute] public ActionResult ViewUserDetails(Guid userId) { // Do something } 如何从MyAttribute中访问userId?有办法可以直接通过吗? 解决方法
您可以尝试使用OnActionExecuting覆盖,您可以在其中进行操作参数的访问.
public class MyAttribute: ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.ActionParameters.ContainsKey("userId")) { var userId = filterContext.ActionParameters["userId"] as Guid; if (userId != null) { // Really?! Great! } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |