gpt4 book ai didi

asp.net-mvc - 您如何返回自定义 503 消息?

转载 作者:行者123 更新时间:2023-12-04 18:35:56 25 4
gpt4 key购买 nike

我有一个在 MVC4/ASP.NET Web Api 中实现的服务。当系统停机维护时,我想向我的客户返回一条自定义 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() - 没有运气

最佳答案

事实证明,您必须配置 http 错误才能通过:

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

关于asp.net-mvc - 您如何返回自定义 503 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10590494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com