APIs

ASP.NET Core provides frameworks for building APIs, gRPC services, and real-time apps with SignalR to instantly push data updates to clients.

Basic Minimal API:

C#Copy

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/hello", () => "Hello, World!");

app.Run();

With the previous code:

  • A minimal API is set up that listens for HTTP GET requests at the /hello URL and responds with “Hello, World!”.
  • The WebApplicationBuilder is used to configure the app.
  • The MapGet method defines a route and a handler for GET requests.

Middleware

ASP.NET Core uses a pipeline of middleware components to handle HTTP requests and responses. This modular approach provides flexibility, allowing you to customize and extend your application’s functionality by adding or removing middleware components as needed.

The middleware pipeline processes HTTP requests in a sequential manner, ensuring that each component can perform its designated task before passing the request to the next component in the pipeline.

Adding built-in middleware in the Program.cs file:

C#Copy

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseHttpsRedirection();

app.UseRouting();

app.MapStaticAssets();

app.UseAuthentication();

app.UseAuthorization();

app.MapGet("/", () => "Hello World!");

app.Run();

In the previous code, several common middleware components were added:

  • UseHttpsRedirection: Redirects HTTP requests to HTTPS.
  • UseRouting: Enables routing to map requests to endpoints.
  • MapStaticAssets: Optimizes the delivery of static files such as HTML, CSS, JavaScript, images and other assets.
  • UseAuthentication: Adds authentication capabilities.
  • UseAuthorization: Adds authorization capabilities.
  • app.MapGet: This is a simple endpoint to demonstrate that the application is running.
networking training courses malaysia

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *