简体   繁体   English

在控制器中查找特定的HttpGet操作

[英]Find specific HttpGet Action in Controller

Consider the following situation. 考虑以下情况。 In my controller I have: 在我的控制器中,我有:

public ActionResult Edit(int id)
{
  ...
}

[HttpPost]
public ActionResult Edit(Model model)
{
  ...
}

Also I have an ActionFilterAttribute , which applies to some other actions of the same controller. 我也有一个ActionFilterAttribute ,它适用于同一控制器的其他一些动作。 In the OnActionExecuting method I need to get the ActionDescriptor of the HttpGet Edit action: OnActionExecuting方法中,我需要获取HttpGet Edit操作的ActionDescriptor

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    // as this is called from the same controller, I use
    ActionDescriptor action = filterContext.ActionDescriptor.ControllerDescriptor
             .FindAction(filterContext.Controller.ControllerContext, "Edit");
    ...
}

The problem is, that the FindAction method returns "reference" to the HttpPost Edit action in case of POST requests. 问题是,在POST请求的情况下, FindAction方法将“引用”返回到HttpPost Edit操作。 How do I make it to look only for HttpGet actions? 如何使其仅查找HttpGet操作?

You can use maybe attribute ? 您可以使用maybe属性吗?

public class FooAttribute
    {


    }

[FooAttribute]
public ActionResult Edit(int id)
{
  ...
}

you can check OnActionExecution; 您可以检查OnActionExecution; example; 例;

var isHasAttribute= filterContext.ActionDescriptor.IsDefined(typeof(FooAttribute), true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM