c# – CreateResponse()和CreateResponse()与Web Api有什么区别吗?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – CreateResponse()和CreateResponse()与Web Api有什么区别吗?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2621字,纯文字阅读大概需要4分钟。
内容图文
![c# – CreateResponse()和CreateResponse()与Web Api有什么区别吗?](/upload/InfoBanner/zyjiaocheng/756/6e234860cc6a48ba96eef1618ae14284.jpg)
鉴于以下内容:
[HttpGet]
[ActionName("GetContent")]
public HttpResponseMessage GetContent(int id)
{
Content content = _uow.Contents.GetById(id);
if (content == null)
{
var message = string.Format("Content with id = {0} not found", id);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
}
else
{
return Request.CreateResponse(HttpStatusCode.OK, content);
}
}
和:
[HttpGet]
[ActionName("GetContent")]
public HttpResponseMessage GetContent(int id)
{
try
{
Content content = _uow.Contents.GetById(id);
if (content == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return Request.CreateResponse<Content>(HttpStatusCode.OK, content);
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
我见过两种编码风格.一个使用例外,另一个不使用.一个使用CreateResponse<>和另一个CreateResponse().有人能说出使用这些有什么优点/缺点吗?据我所知,第二种方法似乎看起来更完整但是真的需要使用try / catch来做这么简单的事情吗?
解决方法:
抛出HttpResponseException的主要好处是当你的action方法返回一个模型类型而不是一个HttpResponseMessage时.例如:
public Product Get(int id)
{
Product p = _GetProduct(id);
if (p == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return p;
}
这相当于以下内容:
public HttpResponseMessage Get(int id)
{
Product p = _GetProduct(id);
if (p == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK, p);
}
选择任何一种风格都可以.
您不应该捕获HttpResponseExceptions,因为重点是Web API管道捕获它们并将它们转换为HTTP响应.在您的第二个代码示例中,当您确实希望客户端接收Not Found(404)时,Not Found错误会被捕获并变为Bad Request错误.
更长的回答:
CreateResponse vs CreateResponse< T>与使用HttpResponseException无关.
CreateResponse返回没有邮件正文的HTTP响应:
public HttpResponseMessage Get()
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
CreateResponse< T>获取类型为T的对象并将该对象写入HTTP响应的主体:
public HttpResponseMessage Get()
{
Product product = new Product();
// Serialize product in the response body
return Request.CreateResponse<Product>(HttpStatusCode.OK, product);
}
下一个示例完全相同,但使用类型推断省略泛型类型参数:
public HttpResponseMessage Get()
{
Product product = new Product();
// Serialize product in the response body
return Request.CreateResponse(HttpStatusCode.OK, product);
}
CreateErrorResponse方法创建一个HTTP响应,其响应主体是一个HttpError对象.这里的想法是使用通用消息格式进行错误响应.调用CreateErrorResponse与此基本相同:
HttpError err = new HttpError( ... )
// Serialize err in the response.
return Request.CreateResponse(HttpStatusCode.BadRequest, err);
内容总结
以上是互联网集市为您收集整理的c# – CreateResponse()和CreateResponse()与Web Api有什么区别吗?全部内容,希望文章能够帮你解决c# – CreateResponse()和CreateResponse()与Web Api有什么区别吗?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。