【问题标题】:Windsor ASP.NET WebAPI serialization errorWindsor ASP.NET WebAPI 序列化错误
【发布时间】:2012-12-05 15:38:33
【问题描述】:

我有一个 Windsor ASP.NET MVC WebAPI 项目设置,例如:

http://nikosbaxevanis.com/2012/07/16/using-the-web-api-dependency-resolver-with-castle-windsor-scoped-lifetime/

当我的控制器返回 OK 状态时,一切正常。但是,如果我尝试发送错误状态,例如 not NotFound,则会收到以下错误:

Castle.MicroKernel.Lifestyle.Scoped.CallContextLifetimeScope+SerializationReference,Castle.Windsor, Version=3.1.0.0, Culture=neutral, 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);
    }

注意,如果我用“resp.Content”注释掉该行,那么一切正常。

【问题讨论】:

    标签: c# asp.net-web-api castle-windsor


    【解决方案1】:
    throw Request.CreateErrorResponse(HttpStatusCode.NotFound, "My custom message");
    

    【讨论】:

    • 杰森,感谢您的回复。 ApiController 没有公开 Repsonse,但我能够让以下代码工作: HttpResponseMessage resp = ControllerContext.Request.CreateResponse(HttpStatusCode.NotFound, "my custom message");抛出新的 HttpResponseException(resp);
    猜你喜欢
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    相关资源
    最近更新 更多