后HTTP标头已被送往服务器不能修改饼干 [英] Server cannot modify cookies after HTTP headers have been sent

查看:587
本文介绍了后HTTP标头已被送往服务器不能修改饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建在C#中的Web应用程序。

I am creating a web application in C#.

在我的页面加载我火了一个异步线程来处理一些数据。该处理的一部分是一个cookie的更新。然而,当我该cookie保存到响应

When my page loads I fire an asynchronous thread to process some data. Part of this processing is the updating of a cookie. However when I save the cookie to the response by

System.Web.HttpContext.Current.Response.Cookies.Add(饼干),我得到了以下异常:

System.Web.HttpContext.Current.Response.Cookies.Add(cookie), I get the following exception:

HttpException:服务器后,HTTP标头已被送往不能修改饼干

什么办法可以解决或解决这一问题?

Any way I can work around or fix this?

推荐答案

除非你有一个很好的理由,你不应该在ASP.NET请求旋转起来后台工作线程。最终你还是要等待这个线程发送响应返回给浏览器之前完成其工作。

Unless you have a very good reason to, you shouldn't be spinning up background worker threads in an ASP.NET request. Ultimately you still have to wait for this thread to finish its work before you send the response back to the browser.

这听起来像响应流已部分写入,然后你的线程试图添加cookie。

It sounds like the response stream has already been partially written to and then your thread is trying to add the cookie.

我想在这里重新考虑你的战略,并采取以下准则读:

I'd rethink your strategy here and take a read of the following guidelines:

第六章 - 提高ASP.NET性能 - 线程指南

它看起来像一个过时的文件,但原则仍然站立。如果让您的来电数据处理器的原因是prevent堵塞,并使用了资源,因为数据处理器长时间运行ASP.NET辅助线程,那么考虑使页面中的异步页来代替。

It looks like a dated document but the principles still stand. If the reason for making your call to the data processor is to prevent the ASP.NET worker thread from blocking and using up resources because the data processor is long running, then consider making the page an Asynchronous page instead.

这篇关于后HTTP标头已被送往服务器不能修改饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆