相关疑难解决方法(0)

在ASP.NET Web API中返回错误的最佳实践

我对我们向客户返回错误的方式感到担忧.

当我们收到错误时,我们通过抛出HttpResponseException立即返回错误:

public void Post(Customer customer)
{
    if (string.IsNullOrEmpty(customer.Name))
    {
        throw new HttpResponseException("Customer Name cannot be empty", HttpStatusCode.BadRequest) 
    }
    if (customer.Accounts.Count == 0)
    {
         throw new HttpResponseException("Customer does not have any account", HttpStatusCode.BadRequest) 
    }
}
Run Code Online (Sandbox Code Playgroud)

或者我们累积所有错误然后发送回客户端:

public void Post(Customer customer)
{
    List<string> errors = new List<string>();
    if (string.IsNullOrEmpty(customer.Name))
    {
        errors.Add("Customer Name cannot be empty"); 
    }
    if (customer.Accounts.Count == 0)
    {
         errors.Add("Customer does not have any account"); 
    }
    var responseMessage = new HttpResponseMessage<List<string>>(errors, HttpStatusCode.BadRequest);
    throw new HttpResponseException(responseMessage);
}
Run Code Online (Sandbox Code Playgroud)

这只是一个示例代码,无论是验证错误还是服务器错误都无关紧要,我只想了解最佳实践,每种方法的优缺点.

c# rest asp.net-web-api

364
推荐指数
8
解决办法
46万
查看次数

我为什么要使用IHttpActionResult而不是HttpResponseMessage?

我一直在使用WebApi进行开发,并已转移到WebApi2,其中Microsoft引入了一个IHttpActionResult似乎建议用于返回a 的新接口HttpResponseMessage.我对这个新接口的优点感到困惑.这似乎主要是公正提供SLIGHTLY更简单的方法来创建一个HttpResponseMessage.

我认为这是"为抽象而抽象"的论点.我错过了什么吗?除了节省一行代码之外,使用这个新接口可以获得什么样的真实优势?

旧方式(WebApi):

public HttpResponseMessage Delete(int id)
{
    var status = _Repository.DeleteCustomer(id);
    if (status)
    {
        return new HttpResponseMessage(HttpStatusCode.OK);
    }
    else
    {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
}
Run Code Online (Sandbox Code Playgroud)

新方式(WebApi2):

public IHttpActionResult Delete(int id)
{
    var status = _Repository.DeleteCustomer(id);
    if (status)
    {
        //return new HttpResponseMessage(HttpStatusCode.OK);
        return Ok();
    }
    else
    {
        //throw new HttpResponseException(HttpStatusCode.NotFound);
        return NotFound();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# httpresponse asp.net-web-api

312
推荐指数
6
解决办法
19万
查看次数

使用HttpClient发布自定义类型

我有一个自定义的dto类:

public class myObject
{
    public string Id { get; set; }
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和使用Web Api的控制器(4.5 .net框架)

[HttpPost]
public IHttpActionResult StripArchiveMailboxPermissions(myObject param)
{
    DoSomething(param);
    return OK();
}
Run Code Online (Sandbox Code Playgroud)

客户端只有4.0 .net框架所以我将无法使用PostAsJsonAsync()方法.将对象从我的客户端传递到服务器的解决方案是什么?

我试过像下面这样的东西:

var response = Client.SendAsync(new HttpRequestMessage<myObject>(objectTest)).Result;
Run Code Online (Sandbox Code Playgroud)

但它抛出了我的异常:

Could not load file or assembly 'Microsoft.Json, Version=2.0.0.0, 
Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. 
The system cannot find the file specified.
Run Code Online (Sandbox Code Playgroud)

是不是可以使用Newtonsoft.Json库?

c# asp.net-web-api dotnet-httpclient

1
推荐指数
1
解决办法
6532
查看次数