Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if request is PartialView or AJAX request in ASP.NET MVC 3

I have to give access rigths to the users of a website. I am doing the filtering here:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
}

The problem is that I cannot distinguish full View request such as 'Index' from PartialViewRequests or AJAX calls requests.

Therefore the page 'Index' has access but the 'PartialViewGridViewForIndex' does not have access.

The property ControllerContext.IsChildAction does not help either.

like image 661
Dragos Durlut Avatar asked Oct 15 '12 08:10

Dragos Durlut


Video Answer


1 Answers

You could use the IsAjaxRequest extension method to determine if an AJAX request was used to invoke this controller action:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (filterContext.HttpContext.Request.IsAjaxRequest())
    {
        // the controller action was invoked with an AJAX request
    }
}
like image 108
Darin Dimitrov Avatar answered Oct 31 '22 14:10

Darin Dimitrov