Tell in ActionFilterAttribute if this is a redirect
I am trying to create an ActionFilterAttribute that only fires once per request, so I am doing something like this.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.IsChildAction)
return;
// More stuff here
}
I may need to check which is also wrong in some cases filterContext.HttpContext.Request.IsAjaxRequest()
, but my real problem is how to tell it is not a redirect. When an activity is X
redirected to an activity Y
, the filter is fired twice, once for X
and once for Y
( IsChildAction
false for Y
).
I tried to store some key on filterContext.HttpContext.Items
, which will tell me that the filter is already running, but those items are not shared between X
and Y
.
Any ideas how I can tell from ActionExecutingContext
what this redirect is?
source to share
I believe that something like this is what you are looking for:
Using MVC action filter to intercept redirects in Ajax requests and return JsonResult
You can check if it is a redirect result or a redirect to route result.
var redirectResult = filterContext.Result as RedirectResult;
if (filterContext.Result is RedirectResult)
{
// It was a RedirectResult => do something
}
else if (filterContext.Result is RedirectToRouteResult)
{
// It was a RedirectToRouteResult => do something
}
source to share