http request does not contain a definition for cre

2019-07-30 07:47发布

问题:

I'm using .net core 2.1 , when I created controller and inherit ApiController. but in my controller method return type HttpResponseMessage and give a error given question title here is my method

[HttpGet]
[Route("GetAll")]
[AcceptVerbs("Get", "Post")]
public HttpResponseMessage SelectCAllCustomers()
{
    try
    {
        return Request.CreateResponse<List<Customer>>(HttpStatusCode.OK, customerDetailsRepository.FindAll("SelectAllCustomers").ToList());
    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
    }
}

回答1:

Asp.Net Core no longer uses that API.

Use the updated syntax

[Route("api/[controller]")]
public class CustomersController: Controller {

    //...

    [HttpGet("GetAll")]
    [AcceptVerbs("Get", "Post")]
    public IActionResult SelectCAllCustomers() {
        var model = customerDetailsRepository.FindAll("SelectAllCustomers").ToList();
        return Ok(model);            
    }
}