Skip to content

Instantly share code, notes, and snippets.

@csharpfritz
Last active June 27, 2016 16:59
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 csharpfritz/fb608b1fd8811e7332fd341bd50685ca to your computer and use it in GitHub Desktop.
Save csharpfritz/fb608b1fd8811e7332fd341bd50685ca to your computer and use it in GitHub Desktop.
ASP.NET Core Controller with MVC and API actions
public class ProductController : Controller {
private readonly ProductRepository _repository;
public ProductController(ProductRepository repository) {
_repository = repository;
}
[Route("/api/Product/{id}")]
public Product Get(int id) {
return _repository.Get(id);
}
[Route("/Product")]
public ActionResult Index() {
var allProducts = _repository.GetAll();
return View(allProducts);
}
}
@DamianEdwards
Copy link

  • Lowercase the _repository
  • Make the _repository readonly
  • Use DI to get the ProductRepository

@csharpfritz
Copy link
Author

Updated as recommended. Thx @DamianEdwards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment