Error System.Web.HttpException when doing a Response.Redirect

0

I have an action filter that validates me if there is a data or this is true or false to then redirect to an external url. Redirection works well. But I have the filter in my controller globally and when I enter the controller for GET it throws an exception for the @Html.AntiForgeryToken() that is in a partial view that I call on my page that I'm entering by GET.

My filter class:

public class LogActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        bool isBloqueado = true;
        if (isBloqueado)
        {
            filterContext.HttpContext.Response.Redirect("https://www.detacoop.cl", true);
            filterContext.HttpContext.ApplicationInstance.CompleteRequest();

            return;
        }

        base.OnActionExecuting(filterContext);
    }
}

My Form that is in a partial view, example: _Footer which is a dependency of my view that I want to render, for example: MyVistaController

@using (Html.BeginForm("LogOff", "Users", FormMethod.Post, new { id = "logoutForm" }))
    {
        @Html.AntiForgeryToken()
        <a href="javascript:document.getElementById('logoutForm').submit()" class="waves-effect waves-light btn-flat u-mar-RL-1 right">Cerrar sesión</a>
    }

And throw me the exception here @Html.AntiForgeryToken()

  

System.Web.HttpException: 'The server can not append a header after sending the HTTP headers.'

Who can help me see why this happens?

    
asked by vcasas 26.06.2018 в 15:41
source

1 answer

0

Maybe you already solved it, or it could be useful to someone else.

I think the error is in Response.Redirect since you should pass the EndResponse in false to not call this method that stops the execution of the page and generates the EndRequest event.

It would stay like this:

filterContext.HttpContext.Response.Redirect("https://www.detacoop.cl", false);

In this link you have more information: MSDN
And the answer to this question may clarify it better.

    
answered by 28.11.2018 в 15:43