Skip to main content

Posts

Showing posts with the label .NET Core

.NET6 Web API CRUD Operation With Entity Framework Core

In this article, we are going to do a small demo on AspNetCore 6 Web API CRUD operations. What Is Web API: Web API is a framework for building HTTP services that can be accessed from any client like browser, mobile devices, desktop apps. In simple terminology API(Application Programming Interface) means an interface module that contains a programming function that can be requested via HTTP calls to save or fetch the data for their respective clients. Some of the key characteristics of API: Supports HTTP verbs like 'GET', 'POST', 'PUT', 'DELETE', etc. Supports default responses like 'XML' and 'JSON'. Also can define custom responses. Supports self-hosting or individual hosting, so that all different kinds of apps can consume it. Authentication and Authorization are easy to implement. The ideal platform to build REST full services. Create A .NET6 Web API Application: Let's create a .Net6 Web API sample application to accomplish our

HTTP Logs With 'UseHttpLogging' Middleware[.NET6 Feature]

In this article, we will know about a .NET6 feature that is 'UseHttpLogging' middleware for logging HTTP requests and responses. UseHttpLogging Middleware: The .NET6 providing a built-in middleware for HTTP logs that is 'UseHttpLogging'. So this middleware provide log information on 'HTTP Request and Response Information', 'Headers' and 'Request Body Information', etc. Create A .NET6 Application: This 'UseHttpLogging' middleware can be configured to any .NET application like MVC, Razor Pages, API, etc. Let's create a .Net6 API sample project to accomplish our demo. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any.Net6 application. For this demo, I'm using the 'Visual Studio Code'(using the .NET CLI command) editor. CLI command For Minimal API Project dotnet new webapi -o Your_Project_Name Configure UseHttpLogging Middleware: Now configure the 'UseHttpLogging'

Different HttpClient Techniques To Consume API Calls In Minimal API[.NET6]

In this article, we are going to implement different HttpClient techniques to consume API calls in minimal API. The different HttpClient techniques that we are going to explore are like: Register HttpClient Object Explicitly In DI(Dependency Injection Service) Named Client Type Client HttpRequestMessage Object Create A .NET6 Minimal API Project: Let's create a .Net6 Minimal API sample project to accomplish our demo. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any.Net6 application. For this demo, I'm using the 'Visual Studio Code'(using the .NET CLI command) editor. CLI command For Minimal API Project dotnet new webapi -minimal -o Your_Project_Name Create A Third Party API Response Model: Here I'm going to use a free third-party rest API that is "https://jsonplaceholder.typicode.com/posts". So to receive the response let's create a response model like 'Post.cs'. Program.cs:(Add Post.cs c

Implement IResult For Custom Response In Minimal API[.NET 6]

In this article, we will understand the implementation of IResult for custom response in minimal API[.NET6]. IResult Type: In minimal API to return a custom response, we have to implement the 'Microsoft.AspNetCore.Http.IResult'. class CusomtResult : IResult { public Task ExecuteAsync(HttpContext httpContext) { throw new NotImplementedException(); } } The 'ExecuteAsync' method gets automatically invoked, the only parameter it will have is the 'HttpContext' where we can use to append our custom response type. Create A .NET6 Minimal API: Let's create a .Net6 Minimal API sample project to accomplish our sample. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any .NET6 application. For this demo, I'm using the 'Visual Studio Code'(using .NET CLI commands) editor. CLI command For Minimal API Project dotnet new webapi -minimal -o Your_Project_Name Implementing Custom Response W

.NET Application Controller Actions Now Support Asynchronous Streaming Response[.NET6 Feature]

In this article, we will know about .NET6 feature that is MVC/API controller actions now supports the asynchronous streaming response. Async Stream: A data stream is often retrieved or generates elements asynchronously is 'Async Stream'. Now from .NET6 we can output the async stream of response from the controller's actions. An action method that contains return type 'IAsyncEnumerable' no longer buffers the response content in application memory before it sends to the clients. So not buffering help to reduce the application memory usage when returning large dataset result that can be asynchronously enumerated. Sample Action Method That Returns 'IAyncEnumerable<T>' Results: Let's write a sample code that can return the asynchronous stream of data. [Route("Test")] [HttpGet] public async IAsyncEnumerable<int> GetNumbers() { for (int i = 1; i < 100; i++) { await Task.Delay(10); Console.WriteLine(i); yield return i;

CSS Isolation Of Razor Pages/Views[.NET6 Feature]

In this article, we will know about a .NET6 feature that is CSS isolation of razor pages/views. CSS Isolation: CSS isolation means creating a CSS file like '.cshtml.css' for each razor page/view(.cshtml). CSS isolation helps to avoid dependencies on global styles that will be very hard to maintain and also avoid style conflicts among the nested content. In this isolation process, the style defined in the '.csthm.css' file are scoped and they are applied to respective razor pages/views. For example, any styling that is added in 'index.cshtml.css' file will only apply to 'index.cshtml' page, they won't affect any other page in the application. The .NET framework runtime will bundle all the isolated CSS files(*.csthml.css) in the single file that is '{your_application_name}.styles.css'. So this bundled CSS line will be added automatically at '{Pages/Views}/Shared/_Layout.cshml' by the framework.  The bundle CSS file({your_application}.st

.NET6 Application No Longer Generates A Separate Assembly For Views By The Razor Compiler[.NET6 Feature]

In this article, we will understand a .NET6 feature that is in a .NET6 application no longer generates a separate assembly for views(.cshtml file) by the razor compiler. .NET5 Or Below Version Generates Separate Views(.cshtml) Assembly:  For .NET5 or below versions razor compiler produces a separate views assembly that contained views and pages(.cshtml files) in the UI application. .NET6 No Separate Views Assembly File: From .NET6 razor compiler builds the views and pages(.csthml) into the UI project assembly itself. This change improves the application build performance, enables single file deployment, and enables these types to work for the Hot Reload(Developers option to reflect changes immediately). Video Session: Wrapping Up: Hopefully, I think this article delivered some useful information on a .NET6 feature no separate assembly file generation for views(.cshtml files). I love to have your feedback, suggestions, and better techniques in the comment section below. Supp

Blazor App Handling Errors Using Error Boundaries Component[.NET6 Feature]

In this article, we will know about .NET6 feature that is in Blazor application handling error using the Error Boundaries. ErrorBoundary Component: ErrorBoundary component is an in-built blazor component, that will handle the unhandled error at the UI level. So with the ErrorBoundary component, we can handle the errors at a particular section or area of the page so that the remaining page of the application runs smoothly without any issue. ErrorBoundary is a wrapper component so we can use like: ErrorBoundary can be wrapped around any Blazor component, so it will display content if no error, but if any child component throws an error then it will show the error message. ErrorBoundary can be wrapped around '@Body' element in 'shared/MainLaout.razor', so it means its scope is global(means page-level error handling), so any error occurs inside of any component of the page then the entire page will show an error message. Use ErrorBoundary At Components Level: Let's

.NET 6 Application No Need To Configure Developer Exception Page Middleware Explicitly[.NET6 Feature]

In this article, we will get to know about a feature of .Net 6 that is 'no need to configure developer exception page middleware explicitly because the framework will automatically enable developer exception page for the Development environment'. .NET 5 or Below Version Application Developer Exception Page Middleware: For bugs identification, the developer exception page is very helpful for debugging. For .NET5 or Below version application developer exception page middleware is always configured in the 'Startup.cs' file based on the environmental variable that is 'Development'. Startup.cs: This middleware renders a well-designed exception page with a full exception stack trace as below. .NET6 Application: From .NET6 application no need to configure developer exception page middleware explicitly. By default, the framework will load the developer exception page if the environment variable is 'Development'. Program.cs: But still, we can see the deve