Endpoint routing for ASP.NET Core 3

Endpoint routing, which was also known as a dispatcher in its early conception, was introduced in version 2.2 of ASP.NET Core, and is, by default, recommended for ASP.NET Core 3.

If you have worked with ASP.NET Core 2.0 and earlier versions, you will find that most applications either use RouteBuilder, as shown in previous examples, or route attributes if you are developing APIs, which we will tackle in a later chapter. You will be familiar with UseMVC() and/or UseRouter() methods, which continue to work in ASP.NET Core 3, but endpoint routing is designed to allow developers to work with applications that are not meant to use MVC and still use routing to handle requests.

Here is an example of what you will normally find in the Startup class, Configure method, in applications prior to ASP.NET Core:

            app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "
{controller=Home}/{action=Index}/{id?}");
});

We must note that before we used app.UseMvc, or app.UseRouting in the Configure method, we always had to define services.AddMvc() in the ConfigureServices method.

Compare this with the default implementation in an ASP.NET Core 3 application as follows:

            app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapControllers();
});

With this implementation, we use endpoints instead of MVC, and therefore we need not add MVC specifically to the ConfigureServices method, and that alone makes this implementation a bit more lightweight, cutting out the overhead that comes with MVC, while it is quite important when we are building applications that do not necessarily need to follow the MVC architecture.

Our application is starting to grow, and so are the chances of encountering errors. Let's have a look at how we are going to add error handling to our application in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.117