luc.chante
luc.chante

Reputation: 440

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.

Upvotes: 4

Views: 4591

Answers (1)

Cris
Cris

Reputation: 13351

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
   }
}

Upvotes: 8

Related Questions