Skip to content

Return HTTP status code

Stanislav Molchanovskiy edited this page Nov 15, 2021 · 2 revisions

It is not always convenient to use with the IActionResult in NClient controllers, so you can use the HttpResponseException to return an error object and HTTP status code. To use these exceptions you need to add NClient controllers in ASP.NET startup as follows:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddNClientControllers().WithResponseExceptions();
}

After that, you can use exceptions in methods of your NClient controllers:

public Entity[] Get()
{
    ...
    throw new HttpResponseException(HttpStatusCode.BadRequest, new { Error = "Error message." });
}

For information on how to get HTTP status code, see section Http response.

Clone this wiki locally