I am working on asp.net core version 2.1, I have created a sample API project which works fine but I am unable to modify the status code with a custom message for example:
In Postman:
200 OK
Expecting:
200 Custom_Message
The code that I tried:
[HttpGet]
public IActionResult Get()
{
Response.StatusCode = 200;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "Custom_Message";
return Ok();
}
Postman's current Output:
GitHub Repository
I think you should create your custom class:
public class CustomResult : OkResult
{
private readonly string Reason;
public CustomResult(string reason) : base()
{
Reason = reason;
}
public override void ExecuteResult(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
context.HttpContext.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = Reason;
context.HttpContext.Response.StatusCode = StatusCode;
}
}
Then in your controller method:
[HttpGet]
public IActionResult Get()
{
return new CustomResult("Custom reason phrase");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With