2

In ASP.NET core MVC, how do I return a custom HTTP response, along with a custom HTTP code? I wish to be able to build the response myself, and preferably return it from a controller.

  public IActionResult Index()
    {
          // set custom HTTP response and return
    }

.... someone fetches Index, and I should be able to manually send a response alike below, where I can control every aspect of the response. I can even work with simply returning a string I build myself.

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
Content-Length: 88
Content-Type: text/html
Connection: Closed

How do I do this in ASP.NET Core MVC?

6
  • What version of ASP.NET Core are you using? Are your deriving from ControllerBase or Controller? Commented Oct 28, 2020 at 0:19
  • 1
    You cannot manually set all headers in the response, such as Content-Length and Connection: closed, btw. And the host webserver will usually add its own Server: header (which you'll have no control over unless you specifically configure your outer web-server or reverse-proxy server). Besides, the whole point of using a framework like ASP.NET Core is to avoid having to mess-around with the low-level details of HTTP. Commented Oct 28, 2020 at 0:21
  • Does this answer your question? Best practice to return errors in ASP.NET Web API Commented Oct 28, 2020 at 0:29
  • This is a duplicate of stackoverflow.com/questions/37690114/… Commented Oct 28, 2020 at 0:34
  • @Mick It is not, that question simply asks how to return a status code. I was looking for a way of building my own responses manually and sending them to client. Commented Oct 28, 2020 at 0:42

2 Answers 2

1

Adapting the answer from here...

public IActionResult Index()
{
    return StatusCode((int)HttpStatusCode.MovedPermanently);
}

Another example if you wanted pass back a 400 status code and validation errors in the response body...

public IActionResult Index()
{
    var validationErrors = _repository.Validate()
    return StatusCode((int)HttpStatusCode.BadRequest, validationErrors);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I realised I was a bit tunnelvisioned, the answer is much simpler than I at first expected.

        public void Index()
    {
        Response.StatusCode = 200;
        Response.StartAsync();

        return;
    }

This allows one to simply add data to a Response themselves and send them to the client. I am using ASP.NET Core 3.1 MVC.

1 Comment

"ASP.NET Core MVC" doesn't really exist in ASP.NET Core 3. Anyway, if you're using this approach then you probably should derive from ControllerBase instead of Controller.

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.