ASP.NET Core MVC in Depth
Back to the web with everything you have learned. You will route requests, validate forms, register services for dependency injection, sign users in with cookie authentication, keep session state, and build pages from partial views.
Updated
Routing in ASP.NET Core
- Conventional and Attribute Routing (Journeyman): Conventional routing builds every address from one pattern; attribute routing gives each action its own address with [Route] and [HttpGet]. Constraints such as {id:int} decide which addresses match.
- Route Values and the Query String (Journeyman): Values reach an action from the route (/products/7) or the query string (?page=2). Choose which with the address's meaning, give optional values defaults, and use [FromRoute] and [FromQuery] to be explicit.
Model Binding and Validation
- Model Binding and Validation: an overview (Apprentice): Never trust what a form sends. Validate on the server every time, and in the browser too for quick feedback; ASP.NET Core does both from one set of rules on the model.
- Data Annotations and ModelState (Journeyman): Put validation rules on the model with data annotations such as [Required], [StringLength] and [Range]; check ModelState.IsValid in the action; add your own errors with AddModelError; and show the messages with validation tag helpers.
- Client-Side Validation (Apprentice): Browsers check HTML attributes such as required on their own; ASP.NET Core's validation scripts check your model's data annotations in the browser, from the data-val attributes the tag helpers write.
Dependency Injection in ASP.NET Core
- Registering Services (Journeyman): Register your classes with ASP.NET Core's service container in Program.cs - as singleton, scoped or transient - and it creates them and hands them to every controller that asks in its constructor.
Security and Sessions
- Authentication vs Authorization (Apprentice): Authentication asks who you are; authorization asks what you're allowed to do. A web app proves who you are once, at sign-in, then remembers you with a cookie on every request after.
- Cookie Authentication and [Authorize] (Journeyman): Turn on cookie authentication in Program.cs, sign users in with claims and SignInAsync, protect actions with [Authorize] and roles, and show who's signed in with User.
- Session State (Journeyman): Session state keeps small values on the server for one visitor between requests, such as recently viewed products, found through a session cookie. Turn it on, then SetString, GetString and SetInt32.
Partial Views in MVC
- Mobile-First Layouts with Bootstrap (Journeyman): Build pages that work on a phone first, then widen. Bootstrap's row-cols classes, cards, a collapsing navbar and pagination turn a Razor loop into a responsive product grid.
Exercise: MVC with Dapper
- Exercise: MVC with Dapper (Journeyman): Grow the BooksPlus website - Dapper and dependency injection, attribute routes, a paged grid of partial-view cards, a validated Add a Book form for administrators only, and recently viewed books in the session.