Posts

ASP.NET Core 8 API: How to implement REPR Pattern Endpoints in ASP.NET Core 8 API

Image
ASP.NET Core, is one of the best technologies for building APIs. In the modern world customer focused applications, the design and implementation of APIs is mandatory for fast data communications. The data can be in JSON format or even may be in the form of Binary Stream. In ASP.NET Core 6 onwards we have been introduced with Minimal APIs. The Minimal API eliminates the need for creating controllers. Yes, use of the controller approach is based on the typical MVC design approach. The MVC Controllers involves Views, ViewModels, etc. But in APIs, we use DTOs. Hence, the controller's approach does not really have any advantages because it has tendency to become bloated with numerous dependencies as we increase the number of endpoints, this also make the maintenance as a challenge. The Minimal APIs are the best. The Minimal API approach offers an endpoint that is directly mapped with the HTTP request type, and it provides a highly cohesive approach of dependency management along with r

ASP.NET Core 8: Creating Custom Authentication Handler for Authenticating Users for the Minimal APIs

Image
The ASP.NET Core is the superb technology for building modern web apps. One of the best features of ASP.ET Core is the ease of security implementation and its customization as per the application requirements. The Identity Object model of ASP.NET Core provides extensive customization to the developers so that they can implement the security logic as per the application's requirements where the Identity can be verified against the On-Premises and/or Cloud stores where the Users' credentials are kept.  In this article we will see an implementation of the custom Authentication Handler to handle the authorization of the request to the Minimal APIs. The Figure 1 provides an idea of the implementation Figure 1: The Authentication Handlers in ASP.NET Core Minimal APIs To implement custom authentication handler, we will be using the following classes: AuthenticationSchemeOptions:   This class is used to set the scheme for the authentication calls. The request to the API will be forward

ASP.NET Core 8: Model Validations in Minimal APIs using Endpoint Filters

Image
Minimal API is the great features of ASP.NET Core. This feature provides a better mechanism to create APIs with less complex code than the traditional controllers' approach. Minimal APIs provides simple and clean code to define HTTP Endpoints. In the case of Minimal APIs, we directly focus on the domain implementation rather than controllers and its actions methods. Minimal APIs offers readability, Testability, as well as they offer an easy approach for building APIs for new professionals.  While implementing APIs, it is important to implement the Model Data Validations to make sure that the data received is valid before it is considered for processing. While using the Minimal APIs, we need to use the Fluent Validations to validate the model data. In this article, we will perform the Model Validation using the FluentValidation package as well as using the Endpoint Filter. The Figure 1 will provide an idea of the Validation in ASP.NET Core Minimal API. Figure 1: The Validation Step

ASP.NET Core: Performing the Model Validations in ASP.NET Core using Action Filters

Image
 In this article, we will see how we can validate the model data in ASP.NET Core API. In ASP.NET Core Actions filters  allows  code to run before or after specific stages in the request processing pipeline.  There build-in filters provided in ASP.NET Core for Authorization, Resources, Exception, etc. We can create the  Custom filters those are used to handle cross-cutting concerns. For example, custom error handling, caching, configuration, authorization, and logging. Action Filters are used to avoid duplicating code. Data Validation is the most important and mandatory part of the application development. This prevents the invalid data to be accepted and processed by the application system. In ASP.NET Core, we have data annotations to validate Model properties. We can apply data annotations on Model properties if we have access to Model classes, but what if we do not have access to it, in this case we need to implement validations using Fluent Validation techniques. In .NET Core we hav

ASP.NET Core API: Returning Video File as stream and playing video stream in Angular Client Application

Image
In this article, we explore an implementation that involves serving a video file from an ASP.NET Core API as a stream and playing it within an Angular client application. Our goal is to demonstrate how to create a stream response from the API and seamlessly deliver it to the client.  During the development of server-side APIs, we often encounter scenarios where users need to download files in client applications. However, when dealing with large files, it’s more efficient to use streamed responses. For instance, if the file is a video, rather than downloading it to the client, we can directly play it in the video player within the client  application. In ASP.NET Core, the  FileStreamResult   object is utilized to write stream data to the response. This approach allows sending binary content using a  Stream  instance when returning a file as a  FileStream . Figure 1 illustrates the high-level implementation concept. Figure 1: ASP.NET Core API returning Stream to Angular Client Step 1: C