aspnet-core ,

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

by Abhith Rajan FollowSeptember 16, 2019 · 1 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,

return  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 Code Description ASP.NET Core 2.2
500 Internal Server Error Status500InternalServerError
501 Not Implemented Status501NotImplemented
502 Bad Gateway Status502BadGateway
503 Service Unavailable Status503ServiceUnavailable
504 Gateway Timeout Status504GatewayTimeout
505 HTTP Version Not Supported Status505HttpVersionNotsupported
506 Variant Also Negotiates Status506VariantAlsoNegotiates
507 Insufficient Storage Status507InsufficientStorage
508 Loop Detected Status508LoopDetected
510 Not Extended Status510NotExtended
511 Network Authentication Required Status511NetworkAuthenticationRequired
599 Network 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 FollowBuy 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