简体   繁体   English

ASP.NET Core Web API 响应 - 状态代码与自定义对象

[英]ASP.NET Core Web API response - status codes vs custom object

I have implemented a logic for Response in my Web API projects, and response object looks like this:我在我的 Web API 项目中实现了Response逻辑,响应对象如下所示:

public class Response
{
    public bool IsSuccess { get; set; }
    public object Data { get; set; }
    public string Message { get; set; }
}
  • IsSuccess indicates whether the call is success or failure. IsSuccess指示调用是成功还是失败。
  • Data to be used for further processing when IsSuccess is true Datatrue时用于进一步处理的IsSuccess
  • Message to be used for further processing when IsSuccess is false Messagefalse时用于进一步处理的IsSuccess

I have few questions in mind.我有几个问题。 Please help me with this.请帮我解决一下这个。

  • Is it ok to use custom objects over the status codes?可以在状态代码上使用自定义对象吗?
  • What disadvantages this approach will it have?这种方法会有什么缺点?
  • Should I switch to status codes / ControllerBase return methods instead?我应该改用状态代码/ ControllerBase返回方法吗?

HTTP status codes are a standard. HTTP 状态码是一种标准。 See eg this docu .参见例如这个文档 Nobody is expecting to get a 200 OK with IsSuccess set to false, because 200 OK is a success.没有人期望在 IsSuccess 设置为 false 的情况下获得 200 OK,因为 200 OK 表示成功。 3xx are redirects, 4xx are client errors, 5xx are server errors. 3xx 是重定向,4xx 是客户端错误,5xx 是服务器错误。 Stick to that, do not reinvent the wheel, you'll confuse the clients of your API.坚持这一点,不要重新发明轮子,你会混淆你的 API 的客户端。

However, you can and it's a good practice to include more custom information into your response.但是,您可以在回复中包含更多自定义信息,而且这是一种很好的做法。 Define your response eg like this:定义您的响应,例如:

public class ErrorDetails
{
    public string Message { get; set; }
}

Than set the response code directly on the response object of .net, not on your own:比直接在.net的响应对象上设置响应代码,而不是你自己:

var error = new ErrorDetails { ... };
context.Response.StatusCode = 4xx / 5xx; // and not 200, it's an error! context is HttpContext
await context.Response.WriteAsync(JsonConvert.SerializeObject(error));

Controller methods already have build-in methods for this, so no need to do it the hard way as in the example above either.控制器方法已经为此提供了内置方法,因此也无需像上面的示例中那样艰难地进行操作。 Eg:例如:

this.BadRequest(error);

This will set a 404 on the response object and pass your error object in the payload as additional data.这将在响应对象上设置 404 并将您的错误对象作为附加数据传递到有效负载中。 There is this.Ok() and a bunch of other methods for each situation.每种情况都有 this.Ok() 和一堆其他方法。

HTTP has standardized the structure for representing request and response. HTTP 已经标准化了表示请求和响应的结构。 To that extent, a response has 3 parts - Status, Headers & Body.在这种情况下,响应包含 3 个部分 - 状态、标题和正文。 Please refer here .请参考这里 Each part has a definite purpose.每个部分都有明确的目的。 Since the question is on status codes, I'll restrict myself to it.由于问题是关于状态码的,我会限制自己。

The primary purpose of status codes is to indicate whether the request has been processed correctly or not.状态码的主要目的是表明请求是否被正确处理。 The automation systems and scripts depend on it for branching their decisions.自动化系统和脚本依赖于它来分支他们的决策。

It is important to remember that the model defined will be part of the response body.重要的是要记住,定义的模型将成为响应主体的一部分。 This means that the framework API is built on will still include a default response code - usually, it's a 200 OK.这意味着构建的框架 API 仍将包含默认响应代码 - 通常,它是 200 OK。 If the IsStatus attribute is supposed to act as a replacement for the Status code, if proper care is not taken, the status code and IsStatus may show different values when the API errors out.如果 IsStatus 属性应该充当状态代码的替代品,如果不采取适当的措施,当 API 出错时,状态代码和 IsStatus 可能会显示不同的值。

Finally, I think you are better off representing an ErrorResponse instead.最后,我认为你最好用一个 ErrorResponse 来代替。 Something in the lines of -有点像——

public class ErrorResponse{
    // Application or Service specific code 
    // to identify the error
    public string Code {get; set;}

    // A link to detailed description of the
    // of the error
    public Uri Info {get; set;}

    // A high level friendly message about the
    // error
    public string Message {get; set;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET API中Http状态代码的自定义异常 - Custom exceptions to Http status codes in ASP.NET API 状态码 406 使用 ASP.NET 内核中的格式响应数据 Web API - Status code 406 using Format response data in ASP.NET Core Web API 带有自定义响应包装器的 ASP.NET Core 3.1 Web API 中的 JSON 响应中断 - JSON response breaks in ASP.NET Core 3.1 Web API with custom response wrapper Asp.net Web Api 将响应状态代码设置为数字 - Asp.net Web Api set response status code to number 如何将自定义标头添加到 ASP.NET Core Web API 响应 - How to add custom header to ASP.NET Core Web API response ASP.NET Core 在 Web API 中处理自定义响应/输出格式的方法 - ASP.NET Core ways to handle custom response/output format in Web API Swagger UI 响应示例 Asp.Net Core Web Api - Swagger UI Response Example Asp.Net Core Web Api 返回自定义状态代码在ASP.Net Web API中不起作用 - Returning custom status code is not working in ASP.Net Web API ASP.NET 核心 API Controller 方法中的存储库结果和状态码 - Repository results and status codes in ASP.NET Core API Controller methods 从ASP.NET Web API ASP.NET Core 2返回HTML并获取http状态406 - Return HTML from ASP.NET Web API ASP.NET Core 2 and get http status 406
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM