Posts

Showing posts from April, 2024

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