Taking ASP.NET Core further
Now that you've seen the basics of how models, views, and controllers work together to provide a web application, let's look at some common scenarios, such as passing parameters and annotating models.
Passing parameters using a route value
Back in the HomeController
class, add the following action method. It uses something called the default model binder
to automatically match the id
passed in the route to the parameter named id
in the method.
Tip
Model binders are very powerful, and the default one does a lot for you. For advanced scenarios, you can create your own by implementing the IModelBinder
interface, but that is beyond the scope of this book.
Inside the method, we check to see whether the id
is null, and if so, it returns a 404 status code and message. Otherwise, we can connect to the database and try to retrieve a product using the id
variable. If we find a product, we pass it to a view; otherwise, we return a different 404 status code and message...