Karthik Ravichandran
Karthik Ravichandran

Reputation: 1283

How to show the status code with custom message in c#?

In MVC5, I have used the below code to return the status code with a custom message. It shows the provided message in my output.

return new HttpStatusCodeResult(403, "Not allowed");

In .net core framework, above method is not applicable, so I tried the below method but I didn't find how to pass the custom message.

StatusCode(403) 

It shows the default message as "Forbidden"

How can I provide the custom message in StatusCode? Are any other methods available?

Upvotes: 42

Views: 43425

Answers (5)

 Tims
Tims

Reputation: 541

I looked into the implementation of standard method (thanks to the Resharper's decompiler):

return Ok("Message");

It basically creates new OkObjectResult, providing the value ("Message") to its constructor, and returns that object. In its turn, OkObjectResult is just a derivative from ObjectResult, it has it's own field with default status code (200), retranslates put into its constructor argument (message, or whatever the object you gave) to base constructor (ObjectResult), and assigns value from it's private constant field to base class's property StatusCode, so it's basically kind of a wrapper for ObjectResult.

So, what is the conclusion one can make from all of this: we can return status code with the message in similar fashion using base ObjectResult class:

return new ObjectResult("Your message") {StatusCode = 403};

Upvotes: 36

Ivan Miroshnichenko
Ivan Miroshnichenko

Reputation: 71

You can do something like this if you aren't in a Controller

return new ObjectResult(Message) { StatusCode = (int)StatusCode };

Upvotes: 7

Mikkel R. Lund
Mikkel R. Lund

Reputation: 2627

I found ContentResult to give the most simple and usable results:

private static ActionResult Result(HttpStatusCode statusCode, string reason) => new ContentResult
{
    StatusCode = (int)statusCode,
    Content = $"Status Code: {(int)statusCode}; {statusCode}; {reason}",
    ContentType = "text/plain",
};

Use it by simply returning that call:

return Result(HttpStatusCode.Unauthorized, "Invalid token");

This will result in a 401 with the text Status Code: 401; Unauthorized; Invalid token.

Upvotes: 21

Gary B
Gary B

Reputation: 61

I think @mmushtaq is right - I wonder if your return type on your method is not IActionResult? See code example below (pulled from here). It will return an ObjectResult (not StatusCodeResult) that includes a message

// GET: api/authors/search?namelike=th
[HttpGet("Search")]
public IActionResult Search(string namelike)
{
    var result = _authorRepository.GetByNameSubstring(namelike);
    if (!result.Any())
    {
        return NotFound(namelike);
    }
    return Ok(result);
}


More links and documentation:

View a Microsoft API Tutorial here

ObjectResult (has multiple properties, including StatusCode and Value.)

StatusCodeResult (has only one Int32 property - StatusCode)

Upvotes: 1

anserk
anserk

Reputation: 1320

You can do something like this:

return StatusCode(403, Json("Not allowed."));

Upvotes: 11

Related Questions