Creating a Not found Endpoint
In this lesson, we're going to create a not found endpoint
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

[00:00 - 00:08] Right now if our client hits an endpoint which doesn't exist, they won't see any error message here. But we want to change that.
[00:09 - 00:18] We want them to get a consistent error message in all possible conditions. The idea is to create a new controller which will work as a redirect controller .
[00:19 - 00:36] Whenever an endpoint is not available, it will be redirected to this controller . Let's go back to our project and inside controllers, let's create a new controller and we will call it redirect controller.
[00:37 - 00:44] Again like all the controllers, we will derive it from the base controller. But this one is going to be a little different.
[00:45 - 00:50] We are going to override the route that we will get from the base controller. I'm talking about this route.
[00:51 - 01:10] We will override it over here. So what we'll do is we will write our route on top and inside we will write redirect and slash we will use a placeholder and we'll call it code.
[01:11 - 01:20] Let's import route using Microsoft.aspnet code.mdc. Code here is a placeholder and will be replaced by the status code.
[01:21 - 01:35] Now let's add the middleware to redirect an unknown route to the redirect controller. I will open the startup file and since this is going to be a part of our configure method, order is really important here.
[01:36 - 01:58] So what we will do is keep it on top so we can keep it here. Now we will use app.use status code pages with re-execute this one and inside we will use the parameters so we can write redirect slash and inside curly brackets we will mention the placeholder.
[01:59 - 02:16] Here we will mention zero. Now what will happen is if there is a request for an endpoint that doesn't exist, it will send it to our redirect controller and zero will be replaced by status code which will be 404.
[02:17 - 02:23] Now let's go back to our redirect controller. Here we can write the method public.
[02:24 - 02:58] I action result and we'll simply call it error and inside it will take a code of type integer as parameter and we will simply return new object result and here we can pass the new API response that we created. API response with code which we will get from here.
[02:59 - 03:13] Let's import API.response from API.error response and that's simply it. Whenever there is a request, the code here will be 404 and it will send a 404 response with a message to the client.
[03:14 - 03:32] To make it work let's restart our server and we can go back to postman and try to hit this request again. We get resource not found message and a status code of 404.
[03:33 - 03:39] So we have made this request consistent as well. Let's work on the internal server error in the next lesson.