0

I need help for my project which consists of creating an e-commerce website. I am currently working on orders and when I do my controller (OrdersController) I cannot use "HttpResponseException" nor "Request.CreateResponse". How can I do to be able to use them? These are the comments in:

  • GetOrder: throw new ...
  • PostOrder: HttpResponseMessage message ... // Return response // return Request.Create ...
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ElevenCherryBackEnd.Entities;
using ElevenCherryBackEnd.Helpers;

namespace ElevenCherryBackEnd.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OrdersController : ControllerBase
    {
        private readonly OrdersContext _context;

        public OrdersController(OrdersContext context)
        {
            _context = context;
        }

        // GET: api/Orders
        [HttpGet]
        public IEnumerable<Order> GetOrders()
        {
            return _context.Orders.Where(o => o.Customer == User.Identity.Name);
        }

        // GET: api/Orders/5
        [HttpGet("{id}")]
        public OrderDTO GetOrder(int id)
        {
            Order order = _context.Orders.Include("OrderDetails.Product")
                .First(o => o.Id == id && o.Customer == User.Identity.Name);
            if (order == null)
            {
                //throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
                return null;
            }

            return new OrderDTO()
            {
                Details = from d in order.OrderDetails
                    select new OrderDTO.Detail()
                    {
                        ProductID = d.Product.Id,
                        Product = d.Product.Name,
                        Price = d.Product.Price,
                        Quantity = d.Quantity
                    }
            };
        }

        // POST: api/Orders
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<Order>> PostOrder(OrderDTO dto)
        {
            if (ModelState.IsValid)
            {
                var order = new Order()
                {
                    Customer = User.Identity.Name,
                    OrderDetails = (from item in dto.Details select new OrderDetail() 
                        { ProductId = item.ProductID, Quantity = item.Quantity }).ToList()
                };

                _context.Orders.Add(order);
                _context.SaveChanges();

                //HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, order);
                //return response;
                return order;
            }
            //return Request.CreateResponse(HttpStatusCode.BadRequest);
            return null;
        }
    }
}

Dateka
  • 1
  • 1

1 Answers1

0

First, you can't use HttpResponseException, because it was removed from ASP.NET Core. There is a special method in Controller class that generates error code responses - StatusCode(int statusCode). E.g., to return a 400 Bad Request code:

return StatusCode(StatusCodes.Status400BadRequest);

There are also additional methods to return common status codes. E.g., 400 Bad Request code can also be returned this way:

return BadRequest();

To be able to return these code results as well as objects, use IActionResult as a return type in your method signature:

[HttpGet("{id}")]
public IActionResult GetOrder(int id) { ... }

[HttpPost]
public async Task<IActionResult> PostOrder(OrderDTO dto) { ... }
Igor
  • 600
  • 6
  • 13