Wednesday, June 22, 2016

ASP.NET Core 1.0 RC2 Status Code Handling

[Please note that this post discusses the topic in the context of ASP.NET Core 1.0 RC2]

In this post let’s see how we can easily handle Status Codes (for instance 404: Not Found) in an ASP.NET Core 1.0 web application.

By the framework we are given with 3 options.
  • app.UseStatusCodePages();
  • app.UseStatusCodePagesWithRedirects()
  • app.UseStatusCodePagesWithReExecute()

Let’s go through each of these by creating a sample application. From Visual Studio, create a new project and select ASP.NET Core Web Application (.NET Core) and from the next page select Web Application.
image
ASP.NET Core Web Application (.NET Core)
Once the project is created, open up Startup.cs and navigate to Configure method where you configure the Middleware for your ASP.NET Core Web Application.

There on the IApplicationBuilder, you can see the following three extension methods for handling status codes.
image
Extension Methods for Handling Status Codes
The easy way to generate 404 error is after running the application, navigate to a view which doesn’t exist. Now let’s see how we can handle 404 with each of these extension methods.


1. UseStatusCodePages



This is the most simple among all three options. Just modify the Configure method adding the following.
app.UseStatusCodePages();

Now if you generate a 404 by running the application, this is what you will get.

image
404
You can specify couple of options over there like modifying the response.
app.UseStatusCodePages("text/plain", "It's a : {0}");

image
404

2. UseStatusCodePagesWithRedirects



You can  use this method to specify a separate page (with either relative or absolute URL) as follows.
app.UseStatusCodePagesWithRedirects("/errorpages/{0}.html");
And here you need to add 404.html page inside a folder named “errorpages” under “wwwroot”.

404.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <h1> Not found. </h1>
</body>
</html>
And now if you generate a 404, this is the output.

image
404

And here if you take a closer look at the URL, you can see that it has been changed because the request is redirected to that particular page.


3. UseStatusCodePagesWithReExecute



When you use this approach what will happen is the request will be re executed (what ever the middleware which is defined after this line) and that’s using the given path.
app.UseStatusCodePagesWithReExecute("/statuscode/{0}");
For instance here the request will try to navigate to StatusCode controller and invoke the specified action. We don’t have a MVC controller named StatusCodeController and let’s just add it inside Controllers folder.
public class StatusCodeController : Controller
{
    [HttpGet("statuscode/{code}")]
    public IActionResult Index(int code)
    {
        return View(code);
    }
}
And let’s add the respective View for this action. For that let’s create a folder named StatusCode inside Views folder. There let’s add a MVC View Page named Index.cshtml.
<h1>@Model</h1>
Now let’s run the application and generate a 404.

image
404
Here you can notice that there was no URL redirection has been done.

So that's about it. Do explore ASP.NET Core 1.0.

Happy Coding.

Regards,
Jaliya

1 comment:

  1. Thank you so much for your post! I found your solution very helpful.

    ReplyDelete