Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windsor ASP.NET WebAPI序列化错误_C#_Asp.net Web Api_Castle Windsor - Fatal编程技术网

C# Windsor ASP.NET WebAPI序列化错误

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 以下是我的控制器

我有一个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");