Я только начал работать с ApiController
. Я пытаюсь сделать HTTP GET, отправив идентификатор, но он не работает.
Мой ApiController:
[Route("api/Test")]
public class TestController : ApiController
{
private myEntity db = new myEntity();
[HttpGet]
public HttpResponseMessage GetAll()
{
// Get a list of customers
IEnumerable<Customer> customers = db.Customers.ToList();
// Write the list of customers to the response body
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, customers);
return response;
}
[HttpGet]
public HttpResponseMessage GetById(int id)
{
// Get Customer by id
Customer customer = db.Customers.Where(x => x.Id == id).FirstOrDefault();
HttpResponseMessage response;
if (customer == null)
{
response = Request.CreateResponse(HttpStatusCode.NotFound);
return response;
} else
{
response = Request.CreateResponse(HttpStatusCode.OK, customer);
}
return response;
}
Когда я запускаю его в браузере, метод GetAll
работает отлично. Однако, когда я пытаюсь GetById
:
http://localhost:53198/api/Test/1
Возвращает:
Не найден ресурс HTTP, соответствующий URI запроса http://localhost:53198/api/Test/1
Кто-нибудь знает, что я делаю не так?