简体   繁体   English

ASP.NET MVC 在 HTTP 标头后无法重定向

[英]ASP.NET MVC Cannot redirect after HTTP headers

I am getting this warning in IIS can it be related to requesting cookies from the browser or how I am using Response.Redirect ?我在 IIS 中收到此警告,它是否与从浏览器请求 cookies 或我如何使用Response.Redirect有关? From some research I see suggestion that I need to add Response.Buffer = true;从一些研究中,我看到我需要添加Response.Buffer = true;的建议。 Is that correct?那是对的吗?

Code代码

    public ActionResult GetTemplateById(string TemplateId)
    {    
        string loginUrl = ConfigurationManager.AppSettings["loginUrlCottTemplateId"];
        Request.Cookies.Add(new HttpCookie("ev", "sJlp+HNulad4GaX2RCjjlQ=="));
        if (Request.Cookies["customerguid"] != null)
        {
            HttpCookie cookie = Request.Cookies["customerguid"];
            string cookieValue = cookie.Value;
            cookieValue = cookieValue.Replace("%2D", "-");
        }
        else
        {
            Response.Redirect(loginUrl + TemplateId);

        }

        if (!AuthenticationRep.IsUserValid(Request))
        {
            return Redirect(storeProductLink);

        }
        var memoryStream = new System.IO.MemoryStream(fm.FileData);
        return new FileStreamResult(memoryStream, "application/pdf");
    }

IIS exception IIS 异常

Exception message: Cannot redirect after HTTP headers have been sent.异常消息:发送 HTTP 标头后无法重定向。

Thread information:线程信息:
Thread ID: 213线程 ID:213
Thread account name: IIS APPPOOL.NET v4.5线程账号名:IIS APPPOOL.NET v4.5
Is impersonating: False是否冒充:假

Stack trace:堆栈跟踪:

at System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent)在 System.Web.HttpResponse.Redirect(字符串 url,Boolean endResponse,Z27245B5Z4F永久)

In your ASP.NET MVC application you are redirecting using Response.Redirect(someurl) .在您的 ASP.NET MVC 应用程序中,您使用Response.Redirect(someurl)进行重定向。
When used in an ASP.NET Web Forms application, any code after Response.Redirect(someurl) is never executed.在 ASP.NET Web Forms 应用程序中使用时,从不执行Response.Redirect(someurl)之后的任何代码。 However when you use the same in ASP.NET MVC the code execution will continue even after sending the redirect response.但是,当您在 ASP.NET MVC 中使用相同的代码时,即使在发送重定向响应之后,代码执行也会继续。 To know why, go through this .要知道为什么,go 通过这个

Your code is trying to redirect two times in the same ActionResult, through -您的代码试图在同一个 ActionResult 中重定向两次,通过 -

Response.Redirect(loginUrl + TemplateId); 

and

return Redirect(storeProductLink);    

and I believe that is throwing this error.我相信这是抛出这个错误。

Please change Response.Redirect(loginUrl + TemplateId) to return Redirect(loginUrl + TemplateId) and check if this fixes your problem.请更改Response.Redirect(loginUrl + TemplateId)return Redirect(loginUrl + TemplateId)并检查这是否解决了您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM