在查看ASP.NET Web API中的文章异常处理之后,我对于何时抛出异常vs返回错误响应感到困惑.我还想知道当你的方法返回特定于域的模型而不是HttpResponseMessage... 时是否可以修改响应
所以,在这里回顾一下我的问题,然后是一些带有#s的代码:
HttpResponseMessage而不是具体的域模型,以便可以自定义消息吗?HttpResponseExceptionVS有Request.CreateErrorResponse什么区别?输出到客户端似乎相同......HttpError"包装"错误中的响应消息(是否抛出异常或返回错误响应)?// CASE #1
public Customer Get(string id)
{
var customer = _customerService.GetById(id);
if (customer == null)
{
var notFoundResponse = new HttpResponseMessage(HttpStatusCode.NotFound);
throw new HttpResponseException(notFoundResponse);
}
//var response = Request.CreateResponse(HttpStatusCode.OK, customer);
//response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
return customer;
}
// CASE #2
public HttpResponseMessage Get(string id)
{
var customer = _customerService.GetById(id);
if (customer …Run Code Online (Sandbox Code Playgroud) 下面是我的代码(部分代码),我想将其迁移到asp.net核心.我的问题是"Request.CreateResponse"在下面的代码中不起作用.我谷歌很多,但无法找到任何解决方案.我该如何解决?包Microsoft.AspNet.WebApi.Core也无法正常工作.
[HttpPost]
public HttpResponseMessage AddBusinessUsersToBusinessUnit(MyObject request)
{
return Request.CreateResponse(HttpStatusCode.Unauthorized);
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.