Web API 控制器失败,"对象内容"1"类型无法序列化响应正文



我有一个带有Get的控制器,它返回Ok(data)data是可以使用Json(数据(手动序列化而不会出错的对象列表。但是当我从控制器返回它时,如果我从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)rn   
at System.Text.EncoderNLS.GetBytes(Char* chars, Int32 charCount, 
Byte* bytes, Int32 byteCount, Boolean flush)rn   
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)rn   
at System.IO.StreamWriter.Write(String value)rn   
at Newtonsoft.Json.JsonTextWriter.WriteNull()rn   
at Newtonsoft.Json.JsonWriter.AutoCompleteClose(JsonContainerType type)rn   
at Newtonsoft.Json.JsonWriter.WriteEndObject()rn   
at Newtonsoft.Json.JsonWriter.WriteEnd(JsonContainerType type)rn   
at Newtonsoft.Json.JsonWriter.WriteEnd()rn   
at Newtonsoft.Json.JsonWriter.AutoCompleteAll()rn   
at Newtonsoft.Json.JsonTextWriter.Close()rn   
at Newtonsoft.Json.JsonWriter.Dispose(Boolean disposing)rn   
at Newtonsoft.Json.JsonWriter.System.IDisposable.Dispose()rn   
at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)rn   
at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)rn   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)rn   
at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)rn--- End of stack trace from previous location where exception was thrown ---rn   
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn   
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn   
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,它创建了不存在的字符,序列化程序导致崩溃

最新更新