C# 如何结合使用ServiceStack和自定义RESTAPI实现自定义ResponseStatus
我一直在使用新的ServiceStack API,如下所示:C# 如何结合使用ServiceStack和自定义RESTAPI实现自定义ResponseStatus,c#,rest,
servicestack,C#,Rest,
servicestack,我一直在使用新的ServiceStack API,如下所示: [Route("/me/login/")] public class LoginRequest : IReturn<LoginResponse> { public string password { get; internal set; } public string username { get; internal set; } } public class LoginResponse { pu
[Route("/me/login/")]
public class LoginRequest : IReturn<LoginResponse>
{
public string password { get; internal set; }
public string username { get; internal set; }
}
public class LoginResponse
{
public string token { get; set; }
}
但是,服务器是一个Django REST Api。因此,当我键入一个不正确的密码时,我会得到以下响应:
{
"non_field_errors": [
"Unable to login with the given credentials."
]
}
如何利用新的ServiceStack API检索
非\u字段\u errors
属性中的信息?(我应该覆盖什么?ServiceStack服务客户端仅用于ServiceStack服务,我建议用于调用第三方服务
与该JSON匹配的响应DTO如下所示:
public class LoginResponse
{
public string token { get; set; }
public string[] non_field_errors { get; set; }
}
如果响应返回非200 HTTP错误,则需要从错误体响应中读取JSON,例如:
try
{
var response = baseUrl.CombineWith("me/login")
.PostJsonToUrl(new LoginRequest { ... })
.FromJson<LoginResponse>();
}
catch (Exception ex)
{
HttpStatusCode? errorStatus = ex.GetStatus();
string errorBody = ex.GetResponseBody();
var errorDto = errorBody.FromJson<LoginResponse>();
}
试试看
{
var response=baseUrl.CombineWith(“我/登录”)
.PostJsonToUrl(新登录请求{…})
.FromJson();
}
捕获(例外情况除外)
{
HttpStatusCode?errorStatus=ex.GetStatus();
字符串errorBody=ex.GetResponseBody();
var errorDto=errorBody.FromJson();
}
try
{
var response = baseUrl.CombineWith("me/login")
.PostJsonToUrl(new LoginRequest { ... })
.FromJson<LoginResponse>();
}
catch (Exception ex)
{
HttpStatusCode? errorStatus = ex.GetStatus();
string errorBody = ex.GetResponseBody();
var errorDto = errorBody.FromJson<LoginResponse>();
}