Skip to main content

Posts

Showing posts with the label As.Net Core MVC

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

.NET Core MVC Application File Upload To Physical Location With Buffered Technique

Buffering Technique In File Upload: The server will use its Memory(RAM) or Disk Storage to save the files on receiving a file upload request from the client.  Usage of Memory(RAM) or Disk depends on the number of file requests and the size of the file.  Any single buffered file exceeding 64KB is moved from Memory to a temp file on disk.  If an application receives heavy traffic of uploading files there might be a chance of out of Disk or RAM memory which leads to crash application. So this Buffer technique used for small files uploading. In the following article, we create a sample for the file uploading using .NET Core MVC application. Create The .NET Core MVC Project: Let's create a .NET Core MVC project, here for this sample I'm using Visual Studio Code as below.   Check the link to use the Visual Studio Code for .NET Core Application . IFormFile: Microsoft.AspNetCore.Http.IFormFile used for file upload with buffered technique. On uploading files f

Asp.Net Core MVC Form Validation Techniques

Introduction: Form validations in any applications are like assures that a valid data is storing on servers. All programing frameworks have their own individual implementations for form validations. In Dotnet Core MVC application server-side validations carried on by the models with the help of Data Annotations and the client-side validations carried by the plugin jQuery Unobtrusive Validation. jQuery Unobtrusive Validation is a custom library developed by Microsoft based on the popular library  jQuery Validate . In this article, we are going to learn how the model validation and client-side validation works in Asp.Net Core MVC Application with sample examples. Getting Started: Let's create an Asp.Net Core MVC application project using preferred editors like Microsoft Visual Studio or Microsoft Visual Studio Code. Here I'm using Visual Studio. Let's create an MVC controller and name it as 'PersonController.cs' and add an action method as bel

Dotnet Core MVC Cookie Login Sample Role-based Authorization (Part 2)

Introduction: Authorization means an authenticated user having permission to access specific protected resources. In general, Authorization can be called special permissions. For example in an application, an admin user has all permission to modify the application, a non-admin user might have permission likes read-only content in the application. In the  first article , we have discussed the Cookie-Based login mechanism. This will be a continuation article, here we discuss Authorization. Note: Before reading this article, read  Dotnet Core Cookie Login Sample (Part 1) . Pages Controller: Now add a new controller name as 'PagesController.cs' and to this controller add 3-action methods to show 3 pages in the application. PagesController.cs: namespace CookieAuth.Web.Controllers { [Route("pages")] public class PagesController : Controller { [Route("admin")] [HttpGet] public IActionResult Admin() {

Dotnet Core MVC Cookie Login Sample From Scratch(Part 1)

Introduction: In general ASP.NET Core applications(MVC, RazorPages, WebAPI, Blazor Server Side) use login libraries like ASP.NET Core Identity , IdentityServer4 , OAuth 2.0 , etc. But ASP.NET Core can simply implement login with using Cookie-Based Authentication  without any login libraries. ASP.NET Core Identity  is a membership program that helps to create a login functionality. Its rich library with all default login functionalities, creating users, adding roles, password encryption, and support to social logins like Google, Facebook, Outlook, Twitter, etc. IdentityServer4 Or OAuth 2.0  is the latest login technology. It also gives all login functionalities and support to social logins as well additionally Single Sign-On and   Token-Based user login. Cookie-Based Authentication  is a default login mechanism provided by ASP.NET Core applications. But user creation, adding roles need to be done manually which is not hard to do. In this approach, we don't