如何设置 ServiceStack ResponseStatus StatusCode?
How do I set the ServiceStack ResponseStatus StatusCode?
我开发了一个从我的 ServiceStack 服务中抛出的自定义异常。状态代码和描述映射正确,但内部 'statusCode' 值始终显示为“0”。
以下是我实现异常的方式:
public class TestException : Exception, IHasStatusCode, IHasStatusDescription, IResponseStatusConvertible
{
private readonly int m_InternalErrorCode;
private readonly string m_ArgumentName;
private readonly string m_DetailedError;
public int StatusCode => 422;
public string StatusDescription => Message;
public TestException(int internalErrorCode, string argumentName, string detailedError)
: base("The request was semantically incorrect or was incomplete.")
{
m_InternalErrorCode = internalErrorCode;
m_ArgumentName = argumentName;
m_DetailedError = detailedError;
}
public ResponseStatus ToResponseStatus()
{
return new ResponseStatus
{
ErrorCode = StatusCode.ToString(),
Message = StatusDescription,
Errors = new List<ResponseError>
{
new ResponseError
{
ErrorCode = m_InternalErrorCode.ToString(),
FieldName = m_ArgumentName,
Message = m_DetailedError
}
}
};
}
}
当我从 ServiceStack 服务抛出异常时
throw new TestException(123, "Thing in error", "Detailed error message");
当我在我的客户端(browser/postman 等)中查看响应时,我得到一个 422 的 HTTP 状态代码,并按预期设置了相应的描述(reason/phrase),但是内容(当我指定 ContentType=[= header) 中的 31=] 看起来像这样...
{
"statusCode": 0,
"responseStatus": {
"errorCode": "422",
"message": "The request was semantically incorrect or was incomplete.",
"stackTrace": "StackTrace ommitted for berivity",
"errors": [
{
"errorCode": "123",
"fieldName": "Thing in error",
"message": "Detailed error message"
}
]
}
}
如您在上面的 json 响应中所见,状态代码为“0”。我的问题是 - 如何设置此值?我猜它应该与 HTTP 响应(上面示例中的 422)相同。
更新:感谢 Mythz 指出我的答案
我像这样更新了我的回复基础 class:
public abstract class ResponseBase : IHasResponseStatus, IHasStatusCode
{
private int m_StatusCode;
public int StatusCode
{
get
{
if (m_StatusCode == 0)
{
if (ResponseStatus != null)
{
if (int.TryParse(ResponseStatus.ErrorCode, out int code))
return code;
}
}
return m_StatusCode;
}
set
{
m_StatusCode = value;
}
}
public ResponseStatus ResponseStatus { get; set; }
}
ServiceStack 仅在错误响应中填充 ResponseStatus
DTO,statusCode
属性 inside 您的响应 DTO 是无关紧要的 属性(可能 on your Response DTO) that ServiceStack doesn't handle. The StatusCode
property from IHasStatusCode
interface implemented in Custom Exceptions is only used to populate HTTP Status Code.
我开发了一个从我的 ServiceStack 服务中抛出的自定义异常。状态代码和描述映射正确,但内部 'statusCode' 值始终显示为“0”。
以下是我实现异常的方式:
public class TestException : Exception, IHasStatusCode, IHasStatusDescription, IResponseStatusConvertible
{
private readonly int m_InternalErrorCode;
private readonly string m_ArgumentName;
private readonly string m_DetailedError;
public int StatusCode => 422;
public string StatusDescription => Message;
public TestException(int internalErrorCode, string argumentName, string detailedError)
: base("The request was semantically incorrect or was incomplete.")
{
m_InternalErrorCode = internalErrorCode;
m_ArgumentName = argumentName;
m_DetailedError = detailedError;
}
public ResponseStatus ToResponseStatus()
{
return new ResponseStatus
{
ErrorCode = StatusCode.ToString(),
Message = StatusDescription,
Errors = new List<ResponseError>
{
new ResponseError
{
ErrorCode = m_InternalErrorCode.ToString(),
FieldName = m_ArgumentName,
Message = m_DetailedError
}
}
};
}
}
当我从 ServiceStack 服务抛出异常时
throw new TestException(123, "Thing in error", "Detailed error message");
当我在我的客户端(browser/postman 等)中查看响应时,我得到一个 422 的 HTTP 状态代码,并按预期设置了相应的描述(reason/phrase),但是内容(当我指定 ContentType=[= header) 中的 31=] 看起来像这样...
{
"statusCode": 0,
"responseStatus": {
"errorCode": "422",
"message": "The request was semantically incorrect or was incomplete.",
"stackTrace": "StackTrace ommitted for berivity",
"errors": [
{
"errorCode": "123",
"fieldName": "Thing in error",
"message": "Detailed error message"
}
]
}
}
如您在上面的 json 响应中所见,状态代码为“0”。我的问题是 - 如何设置此值?我猜它应该与 HTTP 响应(上面示例中的 422)相同。
更新:感谢 Mythz 指出我的答案 我像这样更新了我的回复基础 class:
public abstract class ResponseBase : IHasResponseStatus, IHasStatusCode
{
private int m_StatusCode;
public int StatusCode
{
get
{
if (m_StatusCode == 0)
{
if (ResponseStatus != null)
{
if (int.TryParse(ResponseStatus.ErrorCode, out int code))
return code;
}
}
return m_StatusCode;
}
set
{
m_StatusCode = value;
}
}
public ResponseStatus ResponseStatus { get; set; }
}
ServiceStack 仅在错误响应中填充 ResponseStatus
DTO,statusCode
属性 inside 您的响应 DTO 是无关紧要的 属性(可能 on your Response DTO) that ServiceStack doesn't handle. The StatusCode
property from IHasStatusCode
interface implemented in Custom Exceptions is only used to populate HTTP Status Code.