Request.CreateResponse< List< string>>的替代方法(HttpStatusCode.OK,消息) [英] Alternative for Request.CreateResponse<List<string>>(HttpStatusCode.OK, messages)

查看:149
本文介绍了Request.CreateResponse< List< string>>的替代方法(HttpStatusCode.OK,消息)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在api controller中发送响应,如下所示:(.Net Framework 4.5)

I have been sending response in my api controller as following (.Net Framework 4.5)

 public virtual async Task<HttpResponseMessage> Import(Guid Id)
 { 
    ////
    return Request.CreateResponse<List<string>>(HttpStatusCode.OK, messages);
 }

.NET Core 2.0中上述替代方法是什么?

What is the alternative for above in .NET Core 2.0?

我找到了新的HttpResponseMessage(HttpStatusCode.Ok)方式,但是HttpResponseMessage没有constructor接受/接受additional参数作为消息.

I found a way new HttpResponseMessage(HttpStatusCode.Ok) but there is no constructor for HttpResponseMessage that takes / accept additional parameter for message.

那我现在有什么选择?

推荐答案

HttpResponseMessage is a legacy response object from classic ASP.NET WebAPI. It’s not used internally in ASP.NET Core and using it in your controller will only cause a compat layer to run to convert the legacy response type into what ASP.NET Core uses. So you should not use it when writing new code.

相反,在ASP.NET Core中,您返回IActionResult,对于API方法,则返回API终结点返回的实际对象.

Instead, in ASP.NET Core, you return an IActionResult or for API methods the actual object the API endpoint returns.

状态码200 OK是请求的默认状态码,因此您实际上不需要显式返回带有该状态码的响应.在API控制器操作中,您可以仅返回实际对象:

The status code 200 OK is the default status code for requests, so you actually don’t need to explicitly return a response with that status code. In API controller actions, you can just return the actual object:

[HttpGet]
public List<string> GetMessages()
{
    var messages = GetMessagesFromService();

    return messages;
}

ASP.NET Core会自动将其转换为正确的响应,并将结果转换为JSON(默认情况下)并将其放入正文中.因此,您无需在此处创建回复.

ASP.NET Core will automatically convert this into a proper response and convert the result to JSON (by default) and put it in the body. So you don’t need to create your response there.

如果您遇到需要更改状态码的情况,仍然可以执行以下操作,但是只需在以下位置设置响应的状态码即可:

If you find yourself in a situation where you need to change the status code, you can still do the same but just set the response’s status code before:

[HttpPost]
public string CreateMessage(string message)
{
    var message = CreateMessageInService(message);

    // set status code
    Response.StatusCode = 201; // 201 Created

    return message;
}


ControllerBase中还有许多帮助程序方法,这些方法可让您返回显式的HTTP响应.例如 Ok Created NotFound .但是,这些方法要求返回类型为IActionResult,因此您不能将静态类型与该方法的返回类型一起使用:


There are also a bunch of helper methods in ControllerBase that allow you to return explicit HTTP responses. For example Ok, Created, or NotFound. These method require a return type of IActionResult though, so you cannot use static typing with the method’s return type:

[HttpGet]
public IActionResult GetMessages()
{
    var messages = GetMessagesFromService();

    return Ok(messages);
}

这篇关于Request.CreateResponse&lt; List&lt; string&gt;&gt;的替代方法(HttpStatusCode.OK,消息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆