Below is my code (Partial code) and I want to migrate it into asp.net core.
My problem is “Request.CreateResponse” is not working in below code. I google it a lot but unable to find any solution. How can I solve it? Package Microsoft.AspNet.WebApi.Core is also not working.
[HttpPost]
public HttpResponseMessage AddBusinessUsersToBusinessUnit(MyObject request)
{
return Request.CreateResponse(HttpStatusCode.Unauthorized);
}
Thanks in advance.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Finally I make a solution. 🙂
[HttpPost]
public HttpResponseMessage AddBusinessUsersToBusinessUnit(MyObject request)
{
return new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
Method 2
If you just want to return a status code, the following is preferable
//Creates a StatusCodeResult object by specifying a statusCode. //so controller method should return IActionResult return StatusCode(HttpStatusCode.Unauthorized)
See more in this SO answer
Method 3
Instead of returning Request.Create Response, Latest version allows to return Ok for 200 http status code & add a model on your response if you want to
Also Return IActionResult instead of HttpResponseMessage
public IActionResult Method()
{
try
{
return Ok(new TokenResponseModel { Status = "ok", Message = "valid token" });
}
}
Method 4
For Core 2.1+, return ActionResult or IActionResult instead of HttpResponseMessage. This gives you incredible flexibility. Below is the example that also returns content back.
[HttpPost]
public ActionResult<BusinessUsers> AddBusinessUsersToBusinessUnit(MyObject request)
{ var businessUsers = AddBusinessUsers(request);
return Request.CreateResponse(HttpStatusCode.Unauthorized, businessUsers);
}
https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-2.2
In Core 3.0, you must determine all possible return types with ProducesResponseType
https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-3.0
Method 5
Try this out, below is sample HTTP get controller function
using System;
using System.Net.Http;
using System.Net;
using System.Web.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
[HttpGet]
public async Task<ActionResult<string>> GetItem()
{
try
{
var Item = null;
return JsonConvert.SerializeObject(Item);
}
catch (Exception ex)
{
HttpRequestMessage request = new HttpRequestMessage();
HttpResponseMessage response = request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
throw new System.Web.Http.HttpResponseException(response);
}
}
Method 6
Try something like that
public async Task<IActionResult> Method()
{
try
{
var content = someCollectionReturnedFromDatabase();
return Ok(content);
}
catch(Exception ex)
{
_logger.LogWarning(ex.Message, ex);
return BadRequest(ex.Message); // this can be something like notfound
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0