Created
October 12, 2017 12:59
-
-
Save dcomartin/82d8456ad671697aa51abad381ba15e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Route("api/[controller]")] | |
public class CustomersController : Controller | |
{ | |
private readonly CustomerRepository _customerRepository; | |
public CustomersController() | |
{ | |
_customerRepository = new CustomerRepository(); | |
} | |
// GET api/values/5 | |
[HttpGet("{id}")] | |
public IActionResult Get(int id) | |
{ | |
// Returns null if customer doesn't exist. | |
var customer = _customerRepository.GetById(id); | |
if (customer == null) return new StatusCodeResult(404); | |
// This could throw a NullReferenceException if customer is null | |
return new JsonResult(new | |
{ | |
CustomerId = customer.CustomerId, | |
Name = customer.Name | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment