Setting up an ASP.NET Core project

As mentioned in Chapter 1, REST 101 and Getting Started with ASP.NET Core, the MVC pattern is all about separating concerns. It aims to give developers some guidelines to ensure that the different components of the web application are not mixed up. The following is a refresher on the MVC pattern:

  • The Model seeks to define the domain model of our application. It should also be noted that models don't contain any references to our data sources and databases. They describe the entities in our app.

  • The Views part presents the data in the form of HTML pages. In web services, views are not included because the model is serialized in JSON, HTML, or other similar formats. The critical point is that views should not contain logic. They are hard to test and hard to maintain. Over the past few years, views have become increasingly more powerful. The Razor engine, the default view rendering engine provided by ASP.NET Core, has recently made several new features available. It is easy for developers to implement logic in views, but this should be avoided at all costs.
  • The Controllers part of MVC handles requests from users. They take information from the request and update the model. In real business applications, controllers are usually supported by service or repository classes, which add another level to the domain model layer.

Let's have a detailed look at the default ASP.NET Core web API project template. The project uses the model and controller parts of the MVC pattern to serve a simple HTTP response with the content serialized in JSON.

First of all, let's create a new project using the following commands:

dotnet new webapi -n SampleAPI

The execution of the preceding command creates the following folder structure:

.
├── Controllers
│ └── WeatherForecastController.cs
├── Program.cs
├── Properties
│ └── launchSettings.json
├── SampleAPI.csproj
├── Startup.cs
├── WeatherForecast.cs
├── appsettings.Development.json
├── appsettings.json
└── obj

The execution of the dotnet new webapi command creates a new project file called SampleAPI inside a folder with the same name. The following is the resulting SampleAPI.csproj generated by the dotnet new webapi command:

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

The first thing to note is that this project uses the Microsoft.NET.Sdk.Web SDK, which refers to the web application SDK. Furthermore, the .NET Core framework provides different SDKs depending on the purpose of the project we are about to create. For example, in the case of a desktop application, the project will specify another SDK: Microsoft.NET.Sdk.WindowsDesktop. Choosing between different SDKs guarantees developers an excellent level of modularity. Secondly, the project file does not specify any particular dependency except the netcoreapp target framework used by the application.

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

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