.NET

When To Use FirstOrDefault with Linq Expression

FirstOrDefault is a Linq expression that returns the first element of a sequence or a default value (which is usually null) when no element is found. First is similar, but throws an error when no element is found. FirstOrDefault is usually preferred to First because it does not throw an …

.NET

Hosting ASP.NET Core Website with InterServer

Ok, you developed a cool APS.NET Core website. Now, it’s time to get it out to the world. There are many hosting options out there. You can take a more hands-on approach like deploying to AWS or Azure. Or, you can use the hosting service provider without any hustle of …

.NET

Implementing Dependency Injection in ASP.NET Core

Dependency Injection is the heart of clean architecture for an ASP.NET Core application. It is supported out of the box when you create an ASP.NET Core application with the Microsoft.Extensions.DependencyInjection module. In the start up file, you can add the mapping between interface and actual implementation. IServiceCollection has methods to …

.NET

How to Deploy ASP.NET Core Application to Windows with IIS

An ASP.NET Core application is hosted on Kestrel on both Windows and Linux. Kestrel is a web application server (just like Tomcat or NodeJS). It is included by default in ASP.NET Core project templates. Although ASP.NET Core application can run solely run on Kestrel, it is recommended to use it …

.NET

Quickest Way to Enable CORS for ASP.NET Core Web API application

Browser security does not allow other domain to make AJAX requests.This is called same-origin policy which prevents malicious attack from other sites. When you need to allow other sites to make cross-origin requests, we can enable Cross-origin resource sharing (CORS). There is a pretty good documentation about enabling CORS for …

.NET

Unit Testing Entity Framework Database Update logic with NSubstitute

In the previous post (UnitTesting ASP.NET Web API by Mocking DbContext with NSubstitute), we explored how to create a unit test for getting data from database with Entity Framework by mocking DbContext and DbSet.  We are going to build upon the API we created previously and add PUT logic where …

.NET

Unit Testing ASP.NET Web API by Mocking DbContext with NSubstitute

In the previous post, we created a simpleWeb API in 5 minutes. Now, let’s refactor the code so that it is unit testable with xUnit. The API we build previously was sourcing the data from MySQL database through DbContext. This is an external dependency that we need to replace in …

.NET

Creating Web APIs using ASP.NET and MySQL in 5 minutes

Creating a web API that interact with a database with C# is quite easy thanks to ASP.NET and Entity Framework. Building API with ASP.NET is essentially creating a MVC application without View. If you learn how to create a website with ASP.NET, you can apply the skills directly to API …