Skip to content

Instantly share code, notes, and snippets.

@csharpfritz csharpfritz/ProductController.cs Secret
Last active Jun 27, 2016

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

DamianEdwards commented Jun 27, 2016

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

This comment has been minimized.

Copy link
Owner Author

csharpfritz commented Jun 27, 2016

Updated as recommended. Thx @DamianEdwards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.