Posts

Entity Framework Core (EF) with SQL Server LocalDB

Image
This blog is going to illustrate how to implement Entity Framework (EF) Core with SQL Server LocalDB. The Entity Framework Core enables access to data from the database. This allows developers to avoid having to write most of the data access code. And the SQL Server LocalDB works similarly to the SQL SERVER with few features. You can get the LocalDB when you install visual studio or visual studio express. You can find the .mdf and _log.ldf files in the C:/Users/{user} directory. Let see how to create a SQL Server LocalDB and implement Entity Framework Core Step 1: The first step is to add a connection. In the menu -> click Tools and select Connect to Database It will display the Add Connection window. Enter (localdb)\mssqllocaldb in the server name textbox. Then in the Select or enter a database, select master and click ok Now in the server explorer, you can find the master DB containing Tables, Views, Stored...

SignalR with JavaScript in ASP.NET Core MVC

Image
This blog is going to explain what is SignalR and how to implement it in ASP.NET Core. SignalR is a real time application framework for ASP.NET Core. Using SignalR you can provide updated information without refreshing the page, for example poll results, game score, chat etc. Let's see how to achieve it in ASP.NET Core MVC. Step 1: First step I added the SignalR client library. To add a SignalR client library, Right click on the project in Solution Explorer and Add -> Client-Side Library. Select the provider as unpkg as shown in the image below. Enter the latest version of SignalR into the library. And selected signalr.js and signalr.min.js files. Finally clicked the install button to install the client library. Step 2: At this step I have created a VoteHub.cs class inside the Hubs folder(create a new folder called Hubs). This class inherits from the Hub class which is a base class of SignalR hub. The SendMessage method will be called by the connected client. ...

View-Based Authorization in ASP.NET Core

Image
This blog is going to explain what is View-Based Authorization and how to implement it in ASP.NET Core. In some cases, the developer may need to show or hide the UI based on the user's identity. For example, an admin can edit data, but should not allow guest users to edit data. In this case, the developer should show the edit link when the admin user logs in and hide the edit link when the guest user logs in. This process can be achieved through View-Based Authorization . Let's look at that step by step. Step 1: In the first step, I created a Users class to get user details. Also, I added two values to test View-Based Authorization . For real use, you need to get these user details from the database. Users.cs public class Users { public string Username { get ; set ; } public string Password { get ; set ; } public string Role { get ; set ; } public DateTime DateOfBirth { get ; set ; } public IEnumerable<Users> GetUsers() { ...

Policy-Based Authorization in ASP.NET Core

Image
Authorization is the process of determining whether a user is allowed to access a resource. Policy-based authorization helps to verify one or more requirements to authorize the user. The policy-based authorization helps to separate the application logic and authorization logic We will see step by step how to implement policy-based authorization on ASP.NET Core MVC. In this example, the policy-based authorization is going to authorize whether the user is from the state of Florida or not. This will allow the user to access the page only when the user state is Florida Step 1: In the first step, I created a class to get user information. For demo purposes, I have added two data. But in a real application, you need to get information from the database. public class Users { public string Username { get ; set ; } public string Password { get ; set ; } public string State { get ; set ; } public IEnumerable<Users> GetUsers() { return new ...

Startup Class in ASP.NET Core

Image
The startup class is the heart of the ASP.NET Core. It helps to configure the application. By default, it has three modes: Startup constructor, ConfigureService Method, and Configure Method. The following is a default MVC Startup class. Startup.cs public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get ; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler...

In-Memory Cache in ASP.NET Core

Image
You can improve the performance of your application by using cache. Instead of generating the content, you can retrieve the content from the cache. You can use cache for rarely changing content. Instead of creating content, the cache will hold the content, so it will be available quickly. ASP.NET Core support IMemoryCache, its cache storage in the memory of the webserver. Let's see how this works. The following is a HomeController code. Here the IMemoryCache interface is implemented using dependency injection. You can see in the controller constructor IMemoryCache object has been passed. In coding, the TryGetValue method checks if there is any cached content for the keyword ‘CacheDateKey’, which will provide content if there is any cached content in it. If there is no content in the cache. It will set the cache, i.e. the time for the cache. Also, the SetSlidingExpiration option is set in the example. This indicates how long the cache should be idle. If any user accesses ...

ASP.NET Core Web API Documentation with Swagger

Image
This blog is going to explain how to implement Swagger documentation in ASP.NET Core. Swagger helps to describe API documents so that consumers can easily understand the API. Swagger UI offers a web-based UI. For the developer, this supports creating quick documentation. All public action methods can be tested using the Swagger UI. Step 1: First, you need to create an API project. New Project -> ASP.NET Core web application -> Provide Project Name-> Click Create button-> Select API -> Click Create Button. After that Create a API controller Empty. Step 2: The next step is to install the Swashbuckle package from the Nuget package. If you are using the NuGet UI, search for Swashbuckle.AspNetCore and install it. Otherwise, use the command below if you are using the Package Manager console. Install-Package Swashbuckle.AspNetCore Step 3: The following is a Movie Model. I have added a summary on top of each property to describe the property Movie.cs pub...