0

How to send 400 (or 500) HTTP code with binary content from ASP.NET Core's controller without implementing the IActionResult interface from the ground? Seems like ControllerBase.StatusCode can do the thing, but I can't figure out what the second argument should be.

1
  • 1
    Sending binary data with a 400 or 500 is probably a bad idea, do you really have to do that? Commented Aug 1, 2017 at 8:24

1 Answer 1

1

You do not need to implement IActionResult, you can set the status code, headers and body content manually in your controller. This should work.

[HttpGet("bin")]
public async Task Data()
{
    var data = Encoding.UTF8.GetBytes("Hello world");
    Response.Headers.Add(new KeyValuePair<string, StringValues>("Content-Type", "application/octet-stream"));
    Response.StatusCode = 400;
    await Response.Body.WriteAsync(data, 0, data.Length);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.