Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle differently GET and POST during OnActionExecuting

During the OnActionExecuting method, some processing are made which could lead to a redirection to the home page.

But in Ajax POST calls, these processing will definitely fail. Calls are made by a grid from Kendo UI, so I have no control on them.

So I want this method handles in two different ways if calls are GET and POST.

I tried :

[HttpGet]
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    // Do something
}

[HttpPost]
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    // Do nothing
}

But it does work. I can't find a Property like IsPostBack in WebFroms.

like image 539
luc.chante Avatar asked Feb 13 '23 14:02

luc.chante


1 Answers

The ActionExecutingContext has a HttpContext property. From there, you can obtain the Request property, which has a HttpMethod property

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
   if(filterContext.HttpContext.Request.HttpMethod == "POST")
   {
      // Do nothing
   }
   else
   {
       //Do Something
   }
}
like image 143
Cris Avatar answered Feb 23 '23 08:02

Cris