Hi, I wandered if someone could explain to me the difference between the two methods for returning data from the controller. Is there an advantage or reason for using the method compared to another?
I assume the returned function simply calls Response.WriteAsyncfurther down the line, but I'm not sure.
Using the postman, both methods return the same answer, so I was just curious to know about all these two options, is there a reason to use one over the other or just personal preferences.
Between Response.WriteAsync:
[HttpGet("Fetch_WriteAsync")]
public async Task Fetch_AsyncWrite()
{
HttpContext.Response.ContentType = "application/json";
await HttpContext.Response.WriteAsync(JsonConvert.SerializeObject(new { data = "my_fetched_data" }));
}
and just return:
[HttpGet("Fetch_Return")]
public async Task<string> Fetch_Return()
{
HttpContext.Response.ContentType = "application/json";
return await Task.Factory.StartNew(()=>JsonConvert.SerializeObject(new { data = "my_fetched_data" }));
}
source
share