ASP.NET Core - Return 500 (Internal Server Error) or any other Status Code from API

by Abhith RajanSeptember 16, 2019 · 2 min read · Last Updated:
Share this

In any case if you want to return a status code from your ASP.NET Core API, all you have to do is in your controller method,

1return StatusCode(StatusCodes.Status500InternalServerError);

StatusCode is from Microsoft.AspNetCore.Mvc.ControllerBase.StatusCode and StatusCodes from Microsoft.AspNetCore.Http.StatusCodes. The above code will return a 500 status code. Similar way, you can return any other status code. For the complete list, visit StatusCodes Class (Microsoft.AspNetCore.Http) | Microsoft Docs

HTTP status code 5xx indicates server error. 5xx series includes,

Status CodeDescriptionASP.NET Core 2.2
500Internal Server ErrorStatus500InternalServerError
501Not ImplementedStatus501NotImplemented
502Bad GatewayStatus502BadGateway
503Service UnavailableStatus503ServiceUnavailable
504Gateway TimeoutStatus504GatewayTimeout
505HTTP Version Not SupportedStatus505HttpVersionNotsupported
506Variant Also NegotiatesStatus506VariantAlsoNegotiates
507Insufficient StorageStatus507InsufficientStorage
508Loop DetectedStatus508LoopDetected
510Not ExtendedStatus510NotExtended
511Network Authentication RequiredStatus511NetworkAuthenticationRequired
599Network Connect Timeout Error

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub

Abhith Rajan

Written by Abhith Rajan Buy me a coffee
Abhith Rajan is an aspiring software engineer with more than 6 years of experience and proven successful track record of delivering technology-based products and services.

Related Videos

Related Stories