Posts

Showing posts from July, 2020

Response Caching Middleware Configuration in ASP.NET Core

Image
In this article we are going to see how to configure response caching middleware in ASP.NET Core. The response cache helps to reduce the number of requests for the web server. This is because, when using the response cache on the ASP.NET Core, it receives data from the cache instead of the web server. The response cache attribute [response cache] determines when the response cache should be saved and how long the response cache should be stored. When you first send a request from the client browser, it sends the request to the proxy server, and then it sends the request to the web server. The web server responds to the proxy server with a cache header. The proxy server sends the request to the client browser. For the next subsequent request from the client browser the proxy server will provide the response as long as the cache lifetime. Let us see a small example of the response cache middleware configuration. You must first add response cache middleware to the service collection to u

AutoMapper in ASP.NET Core MVC

Image
AutoMapper maps the object to the object. It maps the value of one object to another. In MVC, often you need to map the value of one object to another. For example, after getting the result from a table, the value must be transferred to another view model object. Only then you can pass the view model to View, to display the value. So Automapper makes your work easier. Automapper is open source, so you can get it with the nuget package manager. The following is a command to install the AutoMapper in your application.  Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection After installation, you can view the AutoMapper under packages. In order for the AutoMapper to work, you first need to add it to the ConfigureServices method in Startup.cs using AutoMapper; public void ConfigureServices(IServiceCollection services) {     services.AddAutoMapper(typeof(Startup));     services

Pass HTML Content from View to Controller using jQuery Ajax

Image
This blog is going to explain how to send a large amount of text content or HTML content from the view to the controller using jQuery Ajax. Ajax means Asynchronous JavaScript and XML. It helps you to load the server data without refreshing the browser. So you can refresh part of the web page without loading the entire page. jQuery Ajax performs asynchronous Ajax requests. The following is a view code. Here I have included the Google jQuery CDN (If you want you can download the jQuery library and include the link). Also, I have added some text boxes and textarea to collect the input values. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <table class="width-hundred-percent">     <tr>         <td> ID: </td>         <td> <input id="idTextBox" type="text" style="width:100%" /> </td>     </tr>     <tr>         <td>Subje

Bind Image src from Controller in ASP.NET Core MVC

Image
This article is going to explain how to bind the img src tag from the Controller value in ASP.NET Core MVC. Let us see this with an example The following is a screenshot of solution explorer. Here the image is stored in wwwroot-> images -> img.png The below is a HomeController code. IWebHostEnvironment is injected in the HomeController constructor to get the absolute path of the image. In the DisplayImage action, the ResponseCache attribute has been added to avoid the image caching.If it is cached, the browser will display the same cached image instead of the new requested image.In the response cache attribute, NoStore = true and Location = ResponseCacheLocation.None are added to avoid request and response cache storage In the DisplayImage action code, the web root path is retrieved from _env.WebRootPath and combined with the image path. Finally it returns the file. public class HomeController : Controller {     private readonly IWebHostEnvironment _env;     public Home

Encrypt/Decrypt Query String in ASP.NET Core MVC

Image
This blog is going to explain how to encrypt and decrypt query string or route data. Let us see with an example For encryption and decryption .Net has IDataProtector interface, which provides data protection services. To use the interface, you have to add the Microsoft.AspNetCore.DataProtection namespace. In the code below, the IDataProtector is injected through the HomeController constructor. In the EncryptValue action, the given input string is encrypted using the _dataProtector.Protect() method. It Cryptographically encrypts the plain text.  And in the DecryptValue action, the encrypted string is decrypted using the _dataProtector.Unprotect() method. It will return the plain protected text. using Microsoft.AspNetCore.DataProtection;    public class HomeController : Controller     {        private readonly IDataProtector _dataProtector;         public HomeController(IDataProtectionProvider provider)         {             _dataProte