3

MVC4/ASP.NET Web API에 구현 된 서비스가 있습니다. 유지 관리를 위해 시스템이 다운 된 경우 고객에게 맞춤형 503 메시지를 보내고 싶습니다.맞춤형 503 메시지는 어떻게 반환합니까?

public class ServiceController : ApiController 
    { 
     public HttpResponseMessage<ServiceUnavailableModel> Get() 
     { 
      return new HttpResponseMessage<ServiceUnavailableModel>(new ServiceUnavailableModel(), HttpStatusCode.ServiceUnavailable); 
     } 
    } 

내 모델은 다음과 같습니다 :

[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); 
     } 

    } 

이 내 현재의 API 클라이언트의 무리가 기대 단순히

나는 다음과 같은 코드가 있습니다. 내 반응이 부분적으로직렬화 된 것을 표시 방법을

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} 

주의 사항 :이 동작을 실행할 때, 나는 다음과 같은 결과를 얻을. 뭐라 구요?

추신. 나는 Response.Clear()와 Response.ClearContent() 해봤 - 행운

+0

ISerializable을 사용하는 이유는 무엇입니까? 모델 클래스는 (그것도'[Serializable]'선언을 제거하면) 괜찮을 것 같습니다. – carlosfigueira

답변

1

다음의 내 대답을 참조하십시오

<configuration> 
    <system.webServer> 
    <httpErrors existingResponse="PassThrough"/> 
    </system.webServer> 
<configuration> 
관련 문제