【问题标题】:Web API controller fails with The 'ObjectContent`1' type failed to serialize the response bodyWeb API 控制器失败,“ObjectContent`1”类型无法序列化响应正文
【发布时间】:2021-01-05 12:24:35
【问题描述】:

我有一个带有 Get 的控制器,它返回 Ok(data)data 是一个对象列表,可以使用 Json(data) 手动序列化而不会出错。但是当我从控制器返回它时,如果我从 Curl 运行,它会失败并出现以下错误:

    {"message":"An error has occurred.","exceptionMessage":"The 'ObjectContent`1' type failed to
     serialize the response body for content type 'application/json; charset=utf-8'.",
    "exceptionType":"System.InvalidOperationException","stackTrace":null,"innerException":
    {"message":"Anerror has occurred.","exceptionMessage":"Unable to translate Unicode character \\uD835 at index 935 
    to specified code page.","exceptionType":"System.Text.EncoderFallbackException","stackTrace":"   
    at System.Text.EncoderExceptionFallbackBuffer.Fallback(Char charUnknown, Int32 index)
    at System.Text.EncoderFallbackBuffer.InternalFallback(Char ch, Char*& chars)   
    at System.Text.UTF8Encoding.GetBytes(Char* chars, Int32 charCount, Byte* bytes, Int32 byteCount, EncoderNLS baseEncoder)\r\n   
    at System.Text.EncoderNLS.GetBytes(Char* chars, Int32 charCount, 
    Byte* bytes, Int32 byteCount, Boolean flush)\r\n   
    at System.Text.EncoderNLS.GetBytes
    (Char[] chars, Int32 charIndex, Int32 charCount, Byte[] bytes, Int32 byteIndex, Boolean flush)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)\r\n   
at System.IO.StreamWriter.Write(String value)\r\n   
at Newtonsoft.Json.JsonTextWriter.WriteNull()\r\n   
at Newtonsoft.Json.JsonWriter.AutoCompleteClose(JsonContainerType type)\r\n   
at Newtonsoft.Json.JsonWriter.WriteEndObject()\r\n   
at Newtonsoft.Json.JsonWriter.WriteEnd(JsonContainerType type)\r\n   
at Newtonsoft.Json.JsonWriter.WriteEnd()\r\n   
at Newtonsoft.Json.JsonWriter.AutoCompleteAll()\r\n   
at Newtonsoft.Json.JsonTextWriter.Close()\r\n   
at Newtonsoft.Json.JsonWriter.Dispose(Boolean disposing)\r\n   
at Newtonsoft.Json.JsonWriter.System.IDisposable.Dispose()\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.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"

我对控制器进行了异常处理,但它没有处理。在 Angular 的客户端,我只看到 (failed)net::ERR_FAILED

如果我用 return Ok() 替换它可以正常工作,我得到 200。

这是我的

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };

        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    }

【问题讨论】:

  • 如果不了解data 是什么,我们无能为力。虽然例外确实说Unable to translate Unicode character \\uD835 at index 935 to specified code page。我相信那个字符是 UTF-16。
  • 你用什么序列化器来手动序列化数据?是DataContractJsonSerializer 吗?
  • 乍一看,似乎代理对在属于该对的两个代码点之间被切断,可能是因为(部分)缓冲区刷新。您可以创建一个包含相关数据的minimal reproducible example 吗?还可能发生的情况是源数据的编码不正确,并且未抛出的代码 (return Json(data)?) 使用空字符串作为无效代码点的后备。
  • 添加了我的WebApiConfig
  • @DavidG 我可以看到,但我在哪里发现错误?

标签: c# json asp.net-web-api


【解决方案1】:

所以问题是我在 Unicode 字符串上使用了 Substring,它正在创建不存在的字符并且序列化程序崩溃了

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 2013-03-28
    • 2016-09-15
    • 2017-07-17
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    相关资源
    最近更新 更多