Reputation: 69
I'm migrating a WebApi to NetCore 2.2, but in netCore 2.2 doesn't exist propety Request and i need it in the fallowing code:
private HttpResponseMessage GetImsResponse(ImsRequest req)
{
try
{
var response = service.Process(req);
if (response is IErrorResponse)
return Request.CreateResponse(System.Net.HttpStatusCode.BadRequest, ((IErrorResponse)response).Message);
if (response != null)
if (response.HasImage())
{
var httpResponse = Request.CreateResponse(System.Net.HttpStatusCode.OK);
httpResponse.Content = new StreamContent((Stream)response.GetImage());
httpResponse.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
return httpResponse;
//Request.CreateResponse(System.Net.HttpStatusCode.Unauthorized, response.Message);
}
else
return Request.CreateResponse(System.Net.HttpStatusCode.OK, response.GetObjectForRepresentInJson());
}
}
ImsRequest is a class implemented by me. And also need it in:
[HttpGet]
public async Task<HttpResponseMessage> Index()
{
return await Task.Factory.StartNew<HttpResponseMessage>(() => GetImsResponse(Request));
}
Upvotes: 0
Views: 666
Reputation: 69
I finally solved, in this case i can change:
return Request.CreateResponse(System.Net.HttpStatusCode.OK, response.GetObjectForRepresentInJson());
TO:
var httpRespose = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
httpRespose.Content = new StringContent("No se pudo obtener un protocolo que resolviera el pedido");
return httpRespose;
Upvotes: 0
Reputation: 239430
What you were doing was never a good way to do it. Just return actual result types from your private method. In Core:
private IActionResult GetImsResponse(ImsRequest req)
{
var response = service.Process(req);
if (response is IErrorResponse)
return BadRequest(((IErrorResponse)response).Message);
if (response != null)
{
if (response.HasImage())
{
return File(response.GetImage(), "image/png");
}
return Ok(response.GetObjectForRepresentInJson());
}
return // return something when response is null
}
Then:
[HttpGet]
public IActionReslt Index()
{
return GetImsResponse(Request);
}
There's nothing async happening here, so using Task.Factory.StartNew
is pointless, especially since you're awaiting it, and would have to await it.
Upvotes: 1