How does ASP.NET Core processes a request?

In this post, I’ll explain how a request comes into the ASP.NET Core web server and how it returns a response to the browser.

If you are aware of how ASP.NET Core processes a request, you can skip reading this article.

The request cycle

ASP.NET CORE request cycle: imitated from the diagram in ASP.NET Core in Action book

When the user tries to access the URL from a browser, the request is sent to a server which then sends it to our reverse proxy server.

As you can see from the above diagram, we first have the reverse proxy intercepting the request before sending it over to the ASP.NET Core web server. We can use the reverse proxy to increase performance, security, load balancing etc.

The request is now forwarded to the ASP.NET Core Web server. Kestrel is the default built-in web server for ASP.NET Core applications.

The responsibility of the Kestrel is to construct a HttpContext object from the raw request data. This object will have details required to create appropriate response to the request.

After processing the application logic, it will return the response to the web server.

The ASP.NET Core web server will convert the response from the application logic into a raw HTTP response and sends it back to the reverse proxy. The reverse proxy will then send it back to the browser.

What is a reverse proxy and why we need one?

A reverse proxy is a proxy that sits in front of the one or more web servers, intercepting the requests.

We don’t want our ASP.NET Core server to handle every type of request from the internet. An example would be if we implement a CDN to server static files when requested for static content, then attacker(s) would target the reverse proxy (in this case the CDN) instead of our server.

Advantages of having a reverse proxy:

  • Load balancing
  • Protection from attacks (DoS)
  • Caching
  • Static content serving
  • API Gateway
  • URL rewriting
  • decoupling the application from the underlying operating system

What is Kestrel web server?

Kestrel web server is a cross-platform open-source web server for ASP.NET Core.

Kestrel is based on Libuv library, the same thing used by NODE.js. Kestrel only uses Libuv for asynchronous I/O work and supports running multiple event loops.

The most important reason Microsoft built kestrel is to provide cross-platform web server which does not rely on System.web and a full version of CLR for bootstrapping the execution environment.