Search code examples
asp.net-corerazor-pagesasp.net-core-8

How can I write the following for the asp.net core 8 razor pages Responses?


I want to write the following code for the asp.net core 8 environment.


 Response.Buffer = true;


 Response.AppendHeader("Content-Disposition", "filename=license.txt");
 
 Response.ContentType = "application/text";

 Response.Output.Write(data);
 Response.Flush();
 
 Response.End();

I managed to add header by the following line.

HttpContext.Response.Headers["Content-Disposition"] = "filename=license.txt";

Solution

  • In your handler method, you only need the following to replace what you posted:

    return File(Encoding.UTF8.GetBytes(data), "application/text", "licence.txt");
    

    You might need to add a using directive for System.Text:

    using System.Text;