Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active October 12, 2017 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/f33bd9b0e04572ba99c884bbd83d5539 to your computer and use it in GitHub Desktop.
Save dcomartin/f33bd9b0e04572ba99c884bbd83d5539 to your computer and use it in GitHub Desktop.
[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);
// 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