Error with AntiForgeryToken - "System.Web.HttpException: 'Server cannot append header after HTTP headers have been sent.'"

Hello

I'm new to programming so forgive for any misuse of words/terms.

I'm using the following code to generate a word document:

using (WordDocument document = new WordDocument())
{                
    IWSection section = document.AddSection();
    IWParagraph paragraph = section.AddParagraph();               
    paragraph.AppendText(" Add Further instructions");                                
    document.Save(NameOfTemplate + ".docx"FormatType.Docx, System.Web.HttpContext.Current.Response, HttpContentDisposition.Attachment);
    document.Close();                
}
The issue comes after this code is executed and I get the following error.

"System.Web.HttpException: 'Server cannot append header after HTTP headers have been sent.'"

I have managed to trace the error down to the following line of code:

document.Save(NameOfTemplate + ".docx"FormatType.Docx, System.Web.HttpContext.Current.Response, HttpContentDisposition.Attachment);
More specifically

System.Web.HttpContext.Current.Response

That being said, I have to use it because this allows for the download of a file into the default downloads folder. How do you get around this error. 


Please advise if you can.

5 Replies

MJ Mohanaselvam Jothi Syncfusion Team May 7, 2020 11:43 AM UTC

Hi Christopher,

Thank you for contacting Syncfusion support.

From the given details, we have found that your end requirement is to download the Word document in application. Please refer the below documentation link to know more about this and you can find sample application along with details from below links:
How to create Word document in ASP.NET MVC
How to create Word document in ASP.NET Core

Please let us know if you have any other questions.

Regards,
Mohanaselvam J 



CJ Christopher Jukes May 7, 2020 03:40 PM UTC

Dear Jothi

Thank you for your mail. That being said, the issue still exists. I was under the impression that it might have been the FormMethod.Get in the following line of code, 

@{Html.BeginForm("GenerateTemplate""IndividualMatterCreateDocument"FormMethod.Get);
Changed it out, the same problem. I also tried to reduce the contents of the word document, but the issue still exists.

I'm sure It has to do with the HttpContent in the following line of code:

document.Save("TestDocument.docx"FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment);
If I use the following line of code, I don't get an error. Please advise

document.Save(@"C:\Test.docx"FormatType.Docx);


MR Manikandan Ravichandran Syncfusion Team May 8, 2020 02:50 PM UTC

Hi Christopher,

Thank you for your update

On further checking, we have found that the
Save(string fileName, FormatType formatType, HttpResponse response, HttpContentDisposition contentDisposition) API is not support in the .NetCore application and it is supported for MVC applications. So, we suggest you to use the below API in .Net core Web application for download the documents after manipulations.

 
document.Save(stream, FormatType.Docx);


//Download Word document in the browser
return File(stream, "application/msword", "Sample.docx");

We have achieved your requirement with the below sample
https://www.syncfusion.com/downloads/support/forum/154020/ze/WebApplication11620169455

Please refer the following UG documentation to know more about Asp.Net Core
https://help.syncfusion.com/file-formats/docio/create-word-document-in-asp-net-core

Please let us know if you have any further questions

Regards,
Manikandan Ravichandran 



PN Preethi Nesakkan Gnanadurai Syncfusion Team May 11, 2020 08:39 AM UTC

From: Chris Jukes Sent: Monday, May 11, 2020 2:05 AM
Subject: Re: Syncfusion support community forum 154020, Error with AntiForgeryToken - "System.Web.HttpException: 'Server cannot append header after HTTP headers have been sent.'", has been updated. 

  

Thank you for your response.   

I did manage a workaround but I'm not sure if it is correct. At the end of my methods, I have changed to return redirectotoaction instead of return view.  

I will go through the above and check whether I can improve my application but please advise whether my change is an acceptable one or whether I will give problems in the future. 

Kind regards 

Christopher Jukes 




MR Manikandan Ravichandran Syncfusion Team May 12, 2020 03:57 PM UTC

Hi Christopher,

Thank you for your update.

Yes, you can use RedirectToAction() Method instead of View() Method to load View page in the browser. There is no impact on changing this line in the project. We have prepared the sample with RedirectToAction() method.which can be downloaded from the below link.
https://www.syncfusion.com/downloads/support/forum/154020/ze/F154020-580630441

If we misunderstood any of your requirement, please share the complete requirement. So that we will validate and provide the solution earlier.

Please let us know if you have any further questions.

Regards,
Manikandan Ravichandran
 


Loader.
Up arrow icon