Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an .asmx web service in same solution with API controllers.

ASMX service had function void and returning customized json response.

Problem is that I am getting error "server cannot clear headers after http headers have been sent".

What I have tried:

private void Json(object obj, int httpcode) 
{
	string objJson = JsonConvert.SerializeObject(obj);

	HttpContext.Current.Response.Clear();
	HttpContext.Current.Response.StatusCode = httpcode;
	HttpContext.Current.Response.TrySkipIisCustomErrors = true;
	HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
	HttpContext.Current.Response.Write(objJson);
	HttpContext.Current.Response.Flush();

	// Gets or sets a value indicating whether to send HTTP content to the client.
	HttpContext.Current.Response.SuppressContent = true;

	// Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
	HttpContext.Current.ApplicationInstance.CompleteRequest();
}
Posted
Comments
MadMyche 18-Jun-18 9:49am    
Which line throws the error? Have you tried rearranging the code in regards to the "SuppressContent"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900