Skip to main content

Posts

Showing posts with the label ASP.NET Core Web API

Configuration And Working Mechanism Of JSON Files On .Net Core Application Starts

.Net Core application JSON file configuration technique has come into picture by replacing traditional way like Web.Config file approach. By default application loads "appSettings.json" file on startup. If we have defined environment variable then application also loads environmental specific json files like "appSettings.Development.json"(for environment variable is Development), "appSettings.Stage.json"(for environment variable is Stage), "appSettings.Production.json"(for environment variable is Production) along with the "appSetting.json". Create A Sample .Net Core Application: Now for better understanding let's walk through with a sample .Net Core Web API application. The application can be created either by using Visual Studio 2019(VS Editor supports .Net Core Version 3.0plus) or  visual Studio Code App Loads appSettings.json And appSettings.{Environmental_Variable}.json By Default: On application start, appSettings.json and ap

.Net Core HttpClient JSON Extension Methods Using System.Net.Http.Json Package

.Net Core 3.0 onwards Microsoft brought up a new package called System.Net.Http.Json. This new package provides JSON extension methods for HttpClient. These JSON extension methods will have a prebuild mechanism for serializing or deserializing response data or payload of HttpClient call. System.Net.Http.Json extension methods that are provided to HttpClient, few of them are mentioned below. GetFromJsonAsync PostAsJsonAsync PutAsJsonAsync ReadFromJsonAsync In this article, we understand System.Net.Http.Json package by implementing the HttpClient samples by with and without JSON extension methods and compare them. Create A .Net Core Web API Sample Application: Let's create a .Net Core sample Web API application, from this application we will consume another Web API by implementing HttpClient calls. We can create a Web API sample application using IDE like Visual Studio 2019(Supports .Net Core 3.0 plus) or  Visual Studio Code . Create A Typed Client: In .Net Core using the Http

.NET Core MVC Application File Upload To Physical Location With Streaming Technique(Useful For Large Files) - Part 2

In  Part 1 we have explored how to upload large files to a server using Streaming Technique. A complete .Net Core sample application has been developed from scratch to understand the file upload. Also discussed on custom model binding manually from the raw form data as well as triggering model validation. I recommend understanding  Part 1 is essential before jumping here.  AJAX Call To File Upload(MVC or Web API): Let's develop an action method that displays a form with a file upload field. Controller/AjaxFileUploadController.cs: using Microsoft.AspNetCore.Mvc; namespace StreamFileUpload.App.Controllers { [Route("ajax-file-upload")] public class AjaxFileUplaodController : Controller { [Route("add-file")] public IActionResult AddFile() { return View(); } } } Let's develop the form that posts the data using the AJAX call. Views/AjaxFileUpload/AddFile.cshtml: <div> <form action="

.NET Core Distributed SQL Server Cache

Introduction: A caching that can be shared or consumed by the multiple apps servers can be called a Distributed Cache. Distributed Cache is an external service, which increases the performance and scalability of applications. The advantages of Distributed Cache are like: No effect on the cache server on an application crash. No need to use local memory. Consistency in serving cached data around multiple applications. In General, Distributed Cache can be achieved by using Redi's Cache, SQL Cache, Third Party Cache Libraries. Here we will learn about .NET Core Distributed SQL Server Cache. Create A .NET Core WebAPI Project: Create a sample .NET Core Web API application to test the Distributed Cache. You can use either Visual Studio 2019 or Visual Studio Code editor, here I'm using Visual Studio Code.  Click here to know basics steps to use Visual Studio Code for .NET Core applications .  SQL Table For Cache: To store the cache data we need a table. So .NET

Dependency Injection In Controller Action Methods

What Is Dependency Injection? : A container or an object which creates and passes the dependant object in an application is called Dependency Injection. In simple words instead of creating dependant objects manually by a programmer, objects are get created by library container which normally handles both the creation and garbage handling. Normally the objects are injected by a mechanism like Property Injection and Constructor Injection(constructor inject most widely used mechanism). From .NET Core, we are getting Dependency Injection as a default feature. With the evolution of .NET Core Microsoft has implemented much more efficient ways of injecting approaches like Constructor Injection, Razor Page Injection(.cshml), and Controller Action Methods Injection(Using Microsoft.AspNetCore.Mvc.FromServices Attribute class). Here I'm going to explain about Controller Action Methods Injection working mechanism comparing with traditional Constructor Injection. A Brief Look

Null Response Behaviour In DotnetCore WebAPI

What HttpNoContentOutputFormatter Do? In DotNet Core Web API by default configured with HttpNoContentOutFormatter. When a NULL result is passed to the 'OK()' method in API, the client consuming that API receives status code 204(NO Content Found) without any response, instead of status code 200 with response NULL value. Sample API endpoint:(returns the NULL result) [Route("Test")] [HttpGet] public IActionResult Test() { string result = null; return Ok(result); } API Result: Returning status 204 is an ideal response for NULL results. But if you want to receive a NULL response with status 200 we can do by removing this HttpNoContentOutputFormatter. Startup.cs:(ConfigureServices method): services.AddControllers(options => { options.OutputFormatters.RemoveType<HttpNoContentOutputFormatter>(); }); API Result: Follow Me: Facebook Twitter Pinterest

GraphQL API Integration In Asp.Net Core Application

Introduction: GraphQL is a query language for your API and a server-side runtime for executing queries by using a type system you define for your data. GraphQL can be integrated into any framework like ASP.NET, Java, NestJs, etc and it isn't tied to any specific database or storage engine and is instead backed by your existing code and data. How GraphQL API Different From Rest API: GraphQL exposes a single end-point or route for the entire application, regardless of its responses or actions. HTTP-POST is the only Http verb recommended by the GraphQL. The client applications (consumers of API) can give instructions to GraphQL API about what type of properties to be returned in the response. Building Blocks Of GraphQL API: The main building blocks of GraphQL API is Schemas and Types.  A 'Schema' in GrpahQL API describes the functionality available to the clients connect to API. Schema mostly consists of GraphQL Object Types, Queries, Mutations, etc. T