How do you return custom message 503?

I have a service implemented in MVC4 / ASP.NET Web Api. I would like to return a custom message to 503 to my clients when the system is not working 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 just what the group of my current API clients expects. However, when I perform 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 seems that my answer was partially serialized. What gives?

PS. I tried Response.Clear () and Response.ClearContent () - no luck

+3
source share
2 answers

Turns out you need to configure HTTP errors to pass:

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

Application_Error Global.asax catch response.status, 503,

, .

Spring MVC 3: HTTP 404?

0

All Articles