How do you return a custom 503 message?

asp.net-mvc, asp.net-mvc-4, asp.net-web-api, http-status-code-503, wcf-web-api

Solution

It turns out you have to configure http errors to pass through:

<configuration>
  <system.webServer>
    <httpErrors existingResponse="PassThrough"/>
  </system.webServer>
<configuration>

Problem

I have a service implemented in MVC4 / ASP.NET Web Api. I would like to return a custom 503 message to my clients when the system is down for maintenance. I have the following code: ``` public class ServiceController : ApiController { public HttpResponseMessage<ServiceUnavailableModel> Get() { return new HttpResponseMessage<ServiceUnavailableModel>(new ServiceUnavailableModel(), HttpStatusCode.ServiceUnavailable); } } ``` My model looks like this: ``` [Serializable] public class ServiceUnavailableModel : ISerializable { public ServiceUnavailableModel() { this.Message = Resources.SDE_SERVICE_UNAVAILABLE; this.Detail = Resources.SDE_SERVICE_REQUESTED_CURRENTLY; this.Code = (int)System.Net.HttpStatusCode.ServiceUnavailable; } public string Message { get; set; } public string Detail { get; set; } public int Code { get; set; } public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("Message", this.Message); info.AddValue("Detail", this.Detail); info.AddValue("Code", this.Code); } } ``` This is simply what a bunch of my current API clients expect. However, when I execute this action, I get the following result: ``` HTTP/1.1 503 Service Unavailable Cache-Control: no-cache Pragma: no-cache Content-Type: text/html Expires: -1 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Mon, 14 May 2012 20:22:39 GMT Content-Length: 200 The service is unavailable.The service you requested is currently unavailable or undergoing maintenance. We will have the service back up and running shortly. Thank you for your patience.","Code":503} ``` Notice how it appears that my response was partially serialized. What gives? PS. I've tried Response.Clear() and Response.ClearContent() - no luck

Original source