Asp.Net Core Web Api - IHttpActionResult

Cenk 1,041 Reputation points
2022-12-20T08:47:16.153+00:00

Hello,

I am upgrading my legacy web API to core. I was using IHttpActionResult in my controller in my legacy code. I wonder if I can use it in core 6 web API?

[HttpPost]  
        [Route("purchase")]  
        public async Task<IHttpActionResult> PurchaseGame(RequestDto game)  
        {  
            if (!ModelState.IsValid) return BadRequest(ModelState);  
  
            HttpResponseMessage gameConfirmResponse;  
            if (game.ProductCode is "132" or "133")  
            {  
                //Call Razer  
                gameConfirmResponse = await _services.RazerPurchase(game);  
            }  
            else  
            {  
                //Call Services  
                gameConfirmResponse = await _services.EzpinPurchase(game);  
            }  
  
  
            //If there are no games  
            if (gameConfirmResponse == null)  
                return new ResponseMessageResult(  
                    Request.CreateErrorResponse((HttpStatusCode)222, new HttpError("No Results Found")));  
  
            if (gameConfirmResponse.Content == null)  
                return new ResponseMessageResult(  
                    Request.CreateErrorResponse((HttpStatusCode)222, new HttpError("No Results Found")));  
  
            switch (gameConfirmResponse.StatusCode)  
            {  
                case HttpStatusCode.NotFound:  
                    {  
                        return NotFound();  
                    }  
                case HttpStatusCode.InternalServerError:  
                    {  
                        return InternalServerError();  
                    }  
                case HttpStatusCode.OK:  
                    {  
                        var responseStream = await gameConfirmResponse.Content.ReadAsStringAsync();  
  
                        var resultResponse = JsonConvert.DeserializeObject<GameConfirmResponse>(responseStream);  
                        if (resultResponse.Coupons == null)  
                            return new ResponseMessageResult(  
                                Request.CreateErrorResponse((HttpStatusCode)222,  
                                    new HttpError("No Coupons Available for this Game")));  
  
  
                        var resultDto = _mapper.Map<GameConfirmResponse, GameConfirmResponseDto>(resultResponse);  
  
  
                        return Ok(resultDto);  
                    }  
                case HttpStatusCode.Unauthorized:  
                    {  
                        return Unauthorized();  
                    }  
                case HttpStatusCode.RequestTimeout:  
                    {  
                        return InternalServerError();  
                    }  
            }  
  
            gameConfirmResponse.Dispose();  
            return Ok(gameConfirmResponse);  
        }  
    }  
Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2022-12-21T06:47:19.887+00:00

    Hi @Cenk ,

    In asp.net core application, you can use the IActionResult to replace the IHttpActionResult:

         public async Task<IActionResult> PurchaseGame(RequestDto game)  
         {  
    

    Then, you can also return BadRequest(), Ok() and Unauthorized().

    For the InternalServerError error, you can return a StatusCode, like this:

        return StatusCode(StatusCodes.Status500InternalServerError, "Error message");  
    

    More detail information, see Controller action return types in ASP.NET Core web API.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 82,061 Reputation points Volunteer Moderator
    2022-12-20T17:19:55.503+00:00

    while you should covert the code, there is a shim to support the old types:

    https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.WebApiCompatShim#supportedframeworks-body-tab


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.