Reputation: 9725
I'm using the following ActionFilter to check if a session variable has been lost, if so they are redirected to the 'select a warehouse code screen'... As far as the user is concerned it is working fine... however ELMAH is reporting an error (see below code) every time I redirect via the ActionFilter.
P.S. This ActionFilter attribute is then put on a base controller, which all other controllers inherit.
P.P.S. There is no other redirect going on... this action filter is firing before the action they tried to perform... I can't for the life of me work out why I keep getting this error.
public class HasWarehouseCodeExpired : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Controller controller = filterContext.Controller as Controller;
if (controller != null)
{
if (SessionHelper.WarehouseCode == null)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "logon", action = "displaywarehouseselection", area = string.Empty }));
}
}
base.OnActionExecuting(filterContext);
}
}
The error:
System.Web.HttpException (0x80004005): Cannot redirect after HTTP headers have been sent. at System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent) at System.Web.HttpResponseWrapper.Redirect(String url, Boolean endResponse) at System.Web.Mvc.RedirectToRouteResult.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
Upvotes: 1
Views: 970
Reputation: 9725
Well I still don't know why the error is occurring, however I stopped the error by adding:
filterContext.HttpContext.Server.ClearError();
Like so:
if (SessionHelper.WarehouseCode == null)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "logon", action = "displaywarehouseselection", area = string.Empty }));
filterContext.HttpContext.Server.ClearError();
}
Upvotes: 2