![](/img/trans.png)
[英]Web API error:The 'ObjectContent`1' type failed to serialize
[英]Web API controller fails with The 'ObjectContent`1' type failed to serialize the response body
我有一個帶有 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();
}
所以問題是我在 Unicode 字符串上使用 Substring 並且它創建了不存在的字符並且序列化程序崩潰了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.