gpt4 book ai didi

c# - 使用 Owin 自主机时从 WebApi Controller 抛出 HttpResponseException

转载 作者:可可西里 更新时间:2023-11-01 09:04:08 25 4
gpt4 key购买 nike

我们正在构建一个使用 Owin 托管的 WebApi。以前,我们在 Controller 操作中使用 HttpResponseException 返回 404 状态代码等,并且运行良好。

但是,当我们开始使用 Owin(自托管)时,我们遇到了这种方法的问题,导致 HttpResponseException 被序列化为 json/xml 并且状态代码从 404 更改为 500(内部服务器错误)。这是我们的代码:

public class InvoicesController : ApiController
{
private readonly IInvoiceRepository _invoiceRepository;

public InvoicesController(IInvoiceRepository invoiceRepository)
{
_invoiceRepository = invoiceRepository;
}

[HttpGet]
public IEnumerable<AccountCodeAssignment> AssignAccountCodesToInvoiceById(int id)
{
var invoice = _invoiceRepository.Get(id);

if (invoice == null) throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Invoice not found"));

yield return new AccountCodeAssignment(1, ...);
yield return new AccountCodeAssignment(2, ...);
yield return new AccountCodeAssignment(3, ...);
yield return new AccountCodeAssignment(4, ...);
}
}

这是我们返回的响应以及 500 响应代码:

{
"Message": "An error has occurred.",
"ExceptionMessage": "Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.",
"ExceptionType": "System.Web.Http.HttpResponseException",
"StackTrace": " at AccountCodeAssignmentService.Controllers.InvoicesController.<AssignAccountCodesToInvoiceById>d__0.MoveNext() in c:\\Projects\\AccountCodeAssignmentService\\Source\\AccountCodeAssignmentService\\Controllers\\InvoicesController.cs:line 38\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Owin.HttpMessageHandlerAdapter.<BufferResponseContentAsync>d__13.MoveNext()"
}

关于我们做错了什么或者在使用 Owin 自托管时不支持 HttpResponseException 有什么想法吗?

编辑:我们使用 WebApi 的一大优势是能够使用和返回我们自己的类型,因此我们希望避免必须更改返回类型。我们目前正在生成 AccountCodeAssignment,因此无法更改返回类型。

最佳答案

我在使用 postman 测试 web api 时遇到过这种情况,请求类型设置为纯文本而不是 application/json。

关于c# - 使用 Owin 自主机时从 WebApi Controller 抛出 HttpResponseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24548313/

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