I have a simple redirect in my code:
public ActionResult Redirect()
{
return Redirect("http://url.com");
}
I noticed that the response includes the following html in the response body:
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Location: http://url.com
Server: Microsoft-IIS/7.0
Date: Tue, 24 Jul 2012 18:53:52 GMT
Content-Length: 198
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://url.com">here</a>.</h2>
</body></html>
Is there a way to remove the html from the response body? I would like the Content-Length to be zero by not having anything in the response body.
You can do this by implementing your own
ActionResultinstead of using the built inRedirectResult, which will send that HTML.However, you should not need to – the user should never see that markup, but it is provided for, mostly, legacy issues. Also be aware some browsers can be set to not follow redirects – not having the body there would then render the result pretty useless.
If you still want a Redirect without body, this result class would do it:
Which would then be used like this: