Reader small image

You're reading from  Software Architecture with C# 12 and .NET 8 - Fourth Edition

Product typeBook
Published inFeb 2024
PublisherPackt
ISBN-139781805127659
Edition4th Edition
Right arrow
Authors (2):
Gabriel Baptista
Gabriel Baptista
author image
Gabriel Baptista

Gabriel Baptista has been working with software development since the beginning of .NET. Today, his main contributions are managing numerous projects for retail and industry. He is an Azure Platform-as-a-Service (PaaS) solution specialist, teaches at Computing Engineering universities, and helps tech startups as a mentor.
Read more about Gabriel Baptista

Francesco Abbruzzese
Francesco Abbruzzese
author image
Francesco Abbruzzese

Francesco Abbruzzese dedicates his life to his two great passions: software and powerlifting. He is the author of the MVC Controls Toolkit and the Blazor Controls Toolkit libraries. He has contributed to the diffusion and evangelization of the Microsoft web stack since the first version of ASP.NET. His company, Mvcct Team, offers web applications, tools, and services for web technologies. He has moved from AI systems, where he implemented one of the first decision support systems for financial institutions, to top-10 video game titles such as Puma Street Soccer.
Read more about Francesco Abbruzzese

View More author details
Right arrow

Presenting ASP.NET Core

In this chapter, you will learn how to implement a web application and a web-based presentation layer. More specifically, you will learn about ASP.NET Core and how to implement a web application presentation layer based on ASP.NET Core MVC.

ASP.NET Core is a .NET framework for implementing web applications. The ASP.NET Core Web API was partially described in previous chapters, so this chapter will focus mainly on ASP.NET Core in general and on ASP.NET Core MVC. More specifically, this chapter will cover the following topics:

  • Understanding the presentation layers of web applications
  • Understanding the basics of ASP.NET Core
  • Understanding how ASP.NET Core MVC creates the response HTML
  • Understanding the connection between ASP.NET Core MVC and design principles

We will review and provide further details on the structure of the ASP.NET Core framework, which we discussed in part in earlier chapters. Here, the main focus is...

Technical requirements

This chapter requires the free Visual Studio 2022 Community edition, ideally with all the database tools installed.

Understanding the presentation layers of web applications

This chapter discusses an architecture for implementing the presentation layers of web applications based on the ASP.NET Core framework. The presentation layers of web applications are based on three techniques:

  • Mobile or desktop native applications that exchange data with servers through REST or SOAP services: We will discuss desktop applications in Chapter 19, Client Frameworks: Blazor.
  • Single-Page Applications (SPAs): These are HTML-based applications whose dynamic HTML is created on the client, either in JavaScript or with the help of WebAssembly (a kind of cross-browser assembly that can be used as a high-performance alternative to JavaScript). Like native applications, SPAs exchange data with the server through HTTP-based APIs, but they have the advantage of being independent of the device and its operating system, since they run in a browser. Chapter 19, Client Frameworks: Blazor, describes the Blazor...

Understanding the basics of ASP.NET Core

ASP.NET Core is based on the concept of the generic host, as explained in the Using generic hosts subsection of Chapter 11, Applying a Microservice Architecture to Your Enterprise Application. The basic architecture of ASP.NET Core was outlined in the A short introduction to ASP.NET Core subsection of Chapter 15, Applying Service-Oriented Architectures with .NET.

It is worth remembering that the host configuration consists mainly of adding services to the Dependency Injection (DI) engine through the Services property of a host builder instance, whose type implements the IServiceCollection interface:

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddTransient<IMyService, MyService>();
...
// Add services to the container through extension methods.
builder.Services.AddControllersWithViews();
builder.Services.AddAllQueries(typeof(ManagePackagesController).Assembly);
...
...
var...

Understanding how ASP.NET Core MVC creates the response HTML

Razor Views

ASP.NET Core MVC uses a language called Razor to define the HTML templates contained in the Views. Razor views are files that are compiled into .NET classes when they’re first used, when the application has been built, or when the application has been published. By default, both pre compilation on each build and on publish are enabled, but you can also enable runtime compilation so that the Views can be modified once they have been deployed. This option can be enabled by checking the Enable Razor runtime compilation checkbox when the project is created in Visual Studio. You can also disable compilation on each build and on publish by adding the following code to the web application project file:

<PropertyGroup>
  <TargetFramework> net8.0 </TargetFramework>
  <!-- add code below -->
  <RazorCompileOnBuild>false</RazorCompileOnBuild>
  <RazorCompileOnPublish...

Understanding the connection between ASP.NET Core MVC and design principles

The whole ASP.NET Core framework is built on top of the design principles and patterns that we analyzed in Chapter 11, Applying a Microservice Architecture to Your Enterprise Application, Chapter 13, Interacting with Data in C# – Entity Framework Core, Chapter 6, Design Patterns and .NET 8 Implementation, Chapter 7, Understanding the Different Domains in Software Solutions, and Chapter 5, Implementing Code Reusability in C# 12.

Moreover, all framework functionalities are provided through DI so that each of them can be replaced by a customized counterpart, without it affecting the remainder of the code. Moreover, these providers are not added individually to the DI engine; instead, they are grouped into collection properties of option objects (see the Loading configuration data and using it with the options framework subsection) for improved maintainability, and to conform to the Separation of Concerns...

Summary

In this chapter, we analyzed the ASP.NET Core pipeline and various modules that comprise an ASP.NET Core MVC application in detail, such as authentication/authorization, the options framework, and routing. Then, we described how controllers and Views map requests to the response HTML. We also analyzed all the improvements introduced in the latest versions.

Finally, we analyzed all the design patterns implemented in the ASP.NET Core MVC framework and, in particular, the importance of the Separation of Concerns principle and how ASP.NET Core MVC implements it in the ASP.NET Core pipeline, as well as in its validation and globalization modules. We focused in more detail on the importance of a Separation of Concerns between the presentation layer logic and graphics, as well as how the MVC pattern ensures it.

You can find a full example of how to use ASP.NET Core MVC in the next chapter, which deals with frontend microservices and describes a complete frontend microservice...

Questions

  1. Can you list all the middleware modules scaffolded by Visual Studio in an ASP.NET Core project?
  2. Does the ASP.NET Core pipeline module need to inherit from a base class or implement some interface?
  3. Is it true that a tag must have just one tag helper defined for it, as, otherwise, an exception is thrown?
  4. Do you remember how to test if validation errors have occurred in a controller?
  5. What is the instruction in a layout view for including the output of the main view?
  6. How are secondary sections of the main view invoked in a layout view?
  7. How does a controller invoke a view?
  8. By default, how many providers are installed in the globalization module?
  9. Are ViewModels the only way for controllers to communicate with their invoked views?

Further reading

...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Software Architecture with C# 12 and .NET 8 - Fourth Edition
Published in: Feb 2024Publisher: PacktISBN-13: 9781805127659
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Gabriel Baptista

Gabriel Baptista has been working with software development since the beginning of .NET. Today, his main contributions are managing numerous projects for retail and industry. He is an Azure Platform-as-a-Service (PaaS) solution specialist, teaches at Computing Engineering universities, and helps tech startups as a mentor.
Read more about Gabriel Baptista

author image
Francesco Abbruzzese

Francesco Abbruzzese dedicates his life to his two great passions: software and powerlifting. He is the author of the MVC Controls Toolkit and the Blazor Controls Toolkit libraries. He has contributed to the diffusion and evangelization of the Microsoft web stack since the first version of ASP.NET. His company, Mvcct Team, offers web applications, tools, and services for web technologies. He has moved from AI systems, where he implemented one of the first decision support systems for financial institutions, to top-10 video game titles such as Puma Street Soccer.
Read more about Francesco Abbruzzese