I am trying to replicate the following ASP.Net code in .Net Core:
return Request.CreateResponse( HttpStatusCode.OK, 100 );
I tried:
using (MemoryStream ms = new MemoryStream()) { using (StreamWriter sw = new StreamWriter(ms)) { sw.Write(100); return new HttpResponseMessage(HttpStatusCode.OK) {Content = new StreamContent(sw.BaseStream)}; } }
but that does not give me the result that I would like. I am working with an outdated API that needs to read an integer from a response stream, so I have no way to change the design.
Below is the code that gets the answer, TryParse always fails, I need it to work out. Unfortunately, I have no way to debug it:
using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string result = reader.ReadToEnd(); int value = 0; if (Int32.TryParse(result, out value)) { return value; } }
source share