0% found this document useful (0 votes)
22 views

Part - 6 Default ASP.NET Core Web API Files and Folders

Uploaded by

SanjeevSonu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Part - 6 Default ASP.NET Core Web API Files and Folders

Uploaded by

SanjeevSonu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Default ASP.

NET Core Web API Files and Folders


In our previous article, we created one ASP.NET Core Web API Project using Visual Studio 2022. We
also see that the project is created with the following files and folder structure.

Now, let us proceed and understand the above files and folders in detail.

Dependencies:
Analyzers:
This folder includes static code analyzers that are integrated into your project. Analyzers examine
your code for style, quality, maintainability, design, and other issues.
 Static Code Analysis: Analyzers perform static code analysis, which means they analyze
your code without actually executing it. This analysis occurs as you write your code, offering
real-time feedback and suggestions within your IDE (like Visual Studio).
 Coding Standards and Style Enforcement: They help enforce coding standards and styles.
This includes ensuring adherence to naming conventions, layout rules, and best practices that
are generally accepted in the .NET community or specific to your organization.
 Identify Common Coding Issues and Bugs: Analyzers can detect common coding
mistakes and potential bugs. For example, they can identify code that may lead to null
reference exceptions, improper disposal of objects, or inefficient use of resources.
 Performance Optimization: Some analyzers are designed to point out areas where code
performance can be improved, such as inefficient loops, unnecessary allocations, or other
resource-intensive operations.
 Refactoring and Code Improvement Suggestions: Analyzers can suggest refactorings or
improvements to make the code more readable, maintainable, or efficient. They can propose
the use of newer language features or more appropriate design patterns.
 Dependency and Package Management: Some analyzers focus on dependencies and
package management, ensuring that you're using the correct versions of NuGet packages
and highlighting potential compatibility issues.
 Framework-Specific Guidelines: In the context of ASP.NET Core Web API, analyzers can
provide recommendations and checks specific to web development, such as proper handling
of HTTP requests and responses, API design guidelines, and best practices for middleware
configuration.
Frameworks:
Here, you will find the .NET Core Frameworks that your project is dependent on. These packages are
essential for running and building ASP.NET Core applications. This includes the base .NET Core
libraries and any specific ASP.NET Core libraries necessary for your Web API project. The framework
contains two folders, i.e., Microsoft.AspNetCore.App and Microsoft.NETCore.App.
 Microsoft.AspNetCore.App: This package (or folder) contains the essential libraries and
components needed for building and running ASP.NET Core applications, including Web
APIs. It includes the ASP.NET Core framework, middleware, and various utility libraries.
 Microsoft.NETCore.App: This package (or folder) contains the runtime components
necessary for running .NET Core applications, including ASP.NET Core applications.
Microsoft.NETCore.App provides the core runtime environment and common libraries that are
required for any .NET Core application to execute. It includes things like the Common
Language Runtime (CLR) and base class libraries.

Packages:
This folder includes NuGet packages that your project depends on. NuGet packages can provide
additional functionality, libraries, or tools that are not part of the standard .

Controllers Folder:
The ASP.NET Core Web API is a Controller-Based Approach. All the controllers of your ASP.NET Core
Web API Application should and must reside inside the Controllers folder. This directory contains the
API controllers that handle incoming HTTP requests. Controllers define action methods that respond
to specific routes and perform the desired logic. Here, by default, one controller is inside the
Controllers folder, called, WeatherForecastController with the following code:
using Microsoft.AspNetCore.Mvc;

namespace MyFirstWebAPIProject.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering",
"Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)


{
_logger = logger;
}

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
appsettings.json file:
The next file that we are going to discuss is the appsettings.json file. This is the same as the
web.config or app.config of our traditional .NET Framework Application. The appsettings.json file is
the application configuration file in the ASP.NET Core Web Application used to store the configuration
settings for your application, such as database connection strings, API keys, and logging settings.

If you open the appsettings.json file, then you will see the following code by default, which was
created by Visual Studio when we created the ASP.NET Core Web API Application.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

MyFirstWebAPIProject.http file
The .http file, in an ASP.NET Core Web API project, is a feature provided by certain Integrated
Development Environments (IDEs) like JetBrains Rider or Visual Studio Code, Visual Studio with the
REST Client extension. These files are used to test HTTP requests directly from the IDE. This
feature is particularly useful for testing and debugging APIs. You will find the following code in the
MyFirstWebAPIProject.http file

How to Use
First run the application using http profile. Please make sure the port number used to launch the
application using http profile and the port number used in the .http file must be same.

Once the application is running, then open the MyFirstWebAPIProject.http file and Click on the
"Send Request" link that appears above the HTTP request in the IDE as shown in the below image:

Program.cs Class File:


In a .NET 8 project, the Program.cs file is the entry point of the application.

public class Program


{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at
https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.


if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
}
}
}

Adding Services to the DI Container:

AddEndpointsApiExplorer: The AddEndpointsApiExplorer() method is used to enable API Explorer


services, which are required for generating the OpenAPI specification. This method adds services
necessary for discovering endpoint metadata. This metadata is essential for tools like Swagger to
generate accurate and useful documentation for your Web API.

AddSwaggerGen: The AddSwaggerGen() method adds Swagger generator services to the


project. These services are responsible for generating Swagger documents, which describe the
structure and capabilities of your Web API. This method allows for customization of the Swagger
documentation, such as setting the title, version, and even adding custom descriptions and schemas.

 app.UseSwagger();: Generates the Swagger document for your API.


 app.UseSwaggerUI();: Serves an interactive UI to visualize and test the API based on
the Swagger document.

You might also like