1

As I have to mirage a project of .Net Framework to that of .Net Core, I have to transform some code in C#.
One kind of code as following

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

XmlWriter writer = XmlWriter.Create(HttpContext.Current.Response.Output, settings);
writer.WriteStartElement("HelloElement");

writer.WriteElementString("Result", "200");
writer.WriteElementString("String1", "Hello");
writer.WriteElementString("String2", "World");
writer.WriteElementString("Data", "data");
writer.WriteEndElement();
writer.Close();

Since HttpContext.Current is not available in .Net Core, I pretty sure I would use HttpContext.Response which instead of HttpContext.Current.Response.
Another difficulty I have to face is that HttpContext.Response.Output is not available.

Could I know a method of transforming the above code? Thank you.

S. Cheng
  • 13
  • 3
  • This code is problematic even in .NET Old. In ASP.NET MVC you should return a FileResult, not write directly to HttpContext.Response. Request and Response are available as [controller properties anyway](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.response?view=aspnetcore-3.1). HttpContext itself is a property of the Controller or PageModel – Panagiotis Kanavos Sep 11 '20 at 09:15
  • Using HttpContext in ASP.NET Core is documented in [Access HttpContext in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-3.1#use-httpcontext-from-a-controller) – Panagiotis Kanavos Sep 11 '20 at 09:17

0 Answers0