-
Notifications
You must be signed in to change notification settings - Fork 1
ASP.NET Core: The MVC Request Life Cycle
Pritesh Patel edited this page Oct 8, 2019
·
4 revisions
https://app.pluralsight.com/courses/a6f34b5b-7124-4a5a-82e8-942f05305965/table-of-contents
- How MVC features work and integrated together (e.g. middleware, routing)
- Stepthrough MVC request lifecycle (request to response)
- MVC technical details have changed significantly from ASP.NET
- How MVC works internally
- How MVC integrates with the larger .NET platform
- How components of MVC process a request
- For debugging, customisation, improved app architecture
- For ASP.NET MVC5 lifecycle - https://app.pluralsight.com/library/courses/mvc-request-life-cycle/table-of-contents
- For ASP.NET Core 3.0 - https://app.pluralsight.com/library/courses/aspnet-core-3-mvc-request-life-cycle/table-of-contents
Need to Know:
- MVC basics Nice to Know:
- Legacy MVC request lifecycle Audience:
- General .NET MVC developers looking to expand knowledge - e.g debugging
- Developers adapted to MVC wanting to know what has changed in MVC Core
- Series of components, events or stages of an app that process a request or respond to user interaction
Program class:
- Configures and runs the web host
- host configuration - content root, IIS integration and Kestral setup; configure Startup class (how the HTTP pipeline is setup)
Startup class:
-
ConfigureServices()- tied to dependency injection -
Configure()- configure core HTTP pipeline by registering middleware components
- Define own
Configure()method with inline middleware components
app.Run(async context =>
{
Debug.WriteLine("=== During Run ===);
await context.Response.WriteAsync("The response from Run.");
});
- Next add
app.Use()call before above section to run before the above component and conditionally pass the request on if it can't handle it itself. On passing the request on to the next component the request can be handled before and after the next component with code before/afterawait next.Invoke() - Typically each middleware component provides some service such as authentication or caching.
- Note there is also the
Map()middleware method
- Create a new middleware class (uses conventions)
- add
public async Task Invoke(HttpContext context) - use
app.Middleware<NewMiddlewareClass>()'to add middleware component to the pipeline inStartup.Configure() - Reuse middleware components across apps and abstract complex functionality into its own class