C# Windsor ASP.NET WebAPI序列化错误
我有一个Windsor ASP.NET MVC WebAPI项目设置,如: 当我的控制器返回OK状态时,一切正常。但是,如果我尝试发送错误状态,如NotFound,则会出现以下错误: Castle.MicroKernel.Lifestyle.Scoped.CallContextLifetimeScope+SerializationReference,Castle.Windsor,版本=3.1.0.0,区域性=中性,PublicKeyToken=407dd0808d44fbdc 以下是我的控制器方法:C# Windsor ASP.NET WebAPI序列化错误,c#,asp.net-web-api,castle-windsor,C#,Asp.net Web Api,Castle Windsor,我有一个Windsor ASP.NET MVC WebAPI项目设置,如: 当我的控制器返回OK状态时,一切正常。但是,如果我尝试发送错误状态,如NotFound,则会出现以下错误: Castle.MicroKernel.Lifestyle.Scoped.CallContextLifetimeScope+SerializationReference,Castle.Windsor,版本=3.1.0.0,区域性=中性,PublicKeyToken=407dd0808d44fbdc 以下是我的控制器
// GET api/values
public IEnumerable<string> Get()
{
bool valid = ...;
if (valid)
return new[] {"valid"};
HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.NotFound);
resp.Content = new StringContent("My custom message");
throw new HttpResponseException(resp);
}
//获取api/值
公共IEnumerable Get()
{
布尔有效=。。。;
如果(有效)
返回新的[]{“有效”};
HttpResponseMessage resp=新的HttpResponseMessage(HttpStatusCode.NotFound);
resp.Content=新的StringContent(“我的自定义消息”);
抛出新的HttpResponseException(resp);
}
请注意,如果我用“resp.Content”注释掉这行,那么一切都正常。Jason,谢谢你的回复。ApiController不公开Repsonse,但我能够让以下代码正常工作:HttpResponseMessageResp=ControllerContext.Request.CreateSponse(HttpStatusCode.NotFound,“我的自定义消息”);抛出新的HttpResponseException(resp);
throw Request.CreateErrorResponse(HttpStatusCode.NotFound, "My custom message");