How to handle null values in ASP.Net Core MVC

Learn to change the default behavior and return HTTP 404 when action methods in ASP.Net Core return null values

How to handle null values in ASP.Net Core MVC
Murat Göçmen / Getty Images

ASP.Net Core MVC is the .Net Core counterpart of the ASP.Net MVC framework. You can take advantage of ASP.Net Core MVC to build cross-platform, scalable, high-performance web applications and APIs using the Model-View-Controller design pattern.

We always want to handle errors elegantly in our web applications. When it comes to empty responses to requests, or returning null values from the action methods, the ASP.NET Core MVC framework returns HTTP Status Code 204, i.e., the “No Content” response. In this article we’ll examine how we can change this default behavior when the action methods return null values.

To work with the code examples illustrates in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here

Create an ASP.Net Core MVC project in Visual Studio

First off, let’s create an ASP.Net Core project in Visual Studio 2019. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new ASP.Net Core project in Visual Studio.

To continue reading this article register now

How to choose a low-code development platform