gpt4 book ai didi

c# - WebApi2 IHttpActionResult 强类型返回值

转载 作者:太空狗 更新时间:2023-10-29 23:20:40 26 4
gpt4 key购买 nike

这样做是可能的,但并不理想:(一个大大简化的例子!)

[Serializable]
public class MyRecord
{
public string key {get; set;}
public string data {get; set;}
}

public async Task<IHttpActionResult> Get(string SomeKey)
{
if(ExistsInDB(SomeKey))
{
return Ok(SomeRecordFromDB(SomeKey)); //SomeRecord() returns a POCO MyRecord.
}
else
{
//I know I can return NotFound() but not the focus of my Q
return Ok(false); //returns "False"
}
}

有效地展示了没有对返回类型进行编译时错误检查。此示例将返回一个 JSon 序列化的 POCO 类(JSON 在我的接受 header 上设置)或者它将返回文本“False”,这将类型检查的负担放在客户端上。

我不会故意这样做,但我的团队正在重构 很多 来自 asmx/svc 的代码并且错误逐渐出现。我喜欢编译器帮助捕获这些类型的错误而不是而不是等待模块或单元测试。

返回到使用强类型方法签名的正确方法是(避免 IHttpActionResult 和 Ok()、NotFound() 等帮助程序,还是有类似 IHttpActionResult<T> 的东西可用于确保正确正在返回类型?

M.

最佳答案

首先,返回IHttpActionResult 是一个很好的做法,表示相应的http status。像这样的东西:

public async Task<IHttpActionResult> Get(string SomeKey)
{
if(ExistsInDB(SomeKey))
return Ok(SomeRecordFromDB(SomeKey));

return NotFound();
}

但是如果你真的想要一个强类型的 api,你可以这样做:

 public async Task<StronglyTypeResponse> Get()
{
return new StronglyTypeResponse();
}

或者,创建一个类型化的响应,您将在响应中保留 http 状态代码等:

public class StronglyTypeResponse
{

}

public class StronglyTypedResult<T> : IHttpActionResult
{
HttpConfiguration _configuration;
T _content;
HttpStatusCode _statusCode;
HttpRequestMessage _request;

public StronglyTypedResult(T content, HttpStatusCode statusCode, HttpRequestMessage request, HttpConfiguration configuration)
{
_content = content;
_request = request;
_configuration = configuration;
_statusCode = statusCode;
}

public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage(_statusCode)
{
Content = new ObjectContent<dynamic>(_content, _configuration.Formatters.JsonFormatter),
RequestMessage = _request,
ReasonPhrase = "some phrase"
};

return Task.FromResult(response);
}
}

然后你可以创建你的方法:

public async Task<StronglyTypedResult<StronglyTypeResponse>> Get()
{
return new StronglyTypedResult<StronglyTypeResponse>(new StronglyTypeResponse(), HttpStatusCode.OK, Request, Configuration);
}

创建您自己的自定义通用 IHttpActionResult

基本相同:

public class IHttpActionResult<T> : System.Web.Http.IHttpActionResult
{
HttpConfiguration _configuration;
T _content;
HttpStatusCode _statusCode;
HttpRequestMessage _request;

public IHttpActionResult(T content, HttpStatusCode statusCode, HttpRequestMessage request, HttpConfiguration configuration)
{
_content = content;
_request = request;
_configuration = configuration;
_statusCode = statusCode;
}

public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage(_statusCode)
{
Content = new ObjectContent<dynamic>(_content, _configuration.Formatters.JsonFormatter),
RequestMessage = _request,
ReasonPhrase = "some phrase"
};

return Task.FromResult(response);
}
}

现在您可以使用类型化的 IHttpActionResult 返回您想要的类:

public async Task<IHttpActionResult<YourClass>> Get()
{
var yourclass = new YourClass();

return new IHttpActionResult<YourClass>(yourclass, HttpStatusCode.OK, Request, Configuration);
}

关于c# - WebApi2 IHttpActionResult 强类型返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40300081/

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