5

我正在尝试HttpResponseException使用“请求”从 POST Web API 操作返回一个:

throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "File not in a correct size"));

这样做时,我得到Value cannot be null. Parameter name: request.

本质上 -请求为空。

我错过了什么?

谢谢

4

7 回答 7

5

我发现我的请求对象在我的 ApiController 中为空,因为我在另一个中嵌套了一个 ApiController 调用。因此,第二个嵌套的 ApiController 从未初始化。您不能手动初始化它,但您可以利用 Request 对象上的 setter 从包装器传递到嵌套的 ApiController。下面是一个模型,在我的真实代码中它修复了我的 Request.CreateErrorResponse(...) 错误。

public class WrapperController : ApiController
{
    // POST api/wrapper
    public void Post(ComplexWithChild value)
    {
        var other = value.otherdata;

        var childcontroller = new ChildController();
        childcontroller.Post(value.child); // ChildController is not initialized, and has null Request
        /*inside ChildController...// causes null reference exception due to null Request
            Request.CreateErrorResponse(HttpStatusCode.BadRequest, "my message"); 
        */

        childcontroller.Request = this.Request;
        childcontroller.Post(value.child); // ChildController uses same Request
        /*inside ChildController...// this works now
            Request.CreateErrorResponse(HttpStatusCode.BadRequest, "my message"); 
        */
    }
}

public class ChildController : ApiController
{
    public void Post(Child value)
    {
        throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "my message"));
    }
}
于 2013-09-23T18:07:53.263 回答
2

用这个:

HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.InternalServerError);
message.Content = new ObjectContent(typeof(MessageResponse), "Invalid Size", GlobalConfiguration.Configuration.Formatters.JsonFormatter);
throw new HttpResponseException(message);

注意:您可以使用您可能希望为用户返回的任何对象更改“无效大小”。例如:

public ErrorMessage
{
     public string Error;
     public int ErrorCode;
}

ErrorMessage msg = new ErrorMessage();
msg.Error = "Invalid Size";
msg.ErrorCode = 500;

HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.InternalServerError);
message.Content = new ObjectContent(typeof(MessageResponse), msg, GlobalConfiguration.Configuration.Formatters.JsonFormatter);
throw new HttpResponseException(message);
于 2013-07-17T17:28:33.837 回答
2

对于像 @DavB.cs 这样嵌套 ApiController 的人的另一种解决方案,例如:

public class ParentController : ApiController
{
    public IHttpActionResult Foo()
    {
       ChildRepository.Foo();
       return Ok(...);
    }
}

public class ChildRepository : ApiController
{
    public HttpResponseMessage Foo()
    {
        // Do something
        return Request.CreateResponse(...);
    }
}

只是简单地从 ParentController 传递请求,如下所示:

public class ParentController : ApiController
    {
        public IHttpActionResult Foo()
        {
           ChildRepository.Foo(Request);
           return Ok(...);
        }
    }

    public class ChildRepository
    {
        public HttpResponseMessage Foo(HttpRequestMessage request)
        {
            // Do something
            return request.CreateResponse(...);
        }
    }

请求来自 ApiController。这将使您摆脱“请求为空”的问题。

希望这可以帮助某人。

于 2016-06-12T03:49:43.123 回答
0

扩展 nizari 的答案,这是我为了包含 ErrorMessage 对象列表所做的:

IEnumerable errorList = new List<ErrorMessage>();

// ...

var error = new HttpError( "There were errors." ) 
{
    { "Errors", errorList }
};

var message = new HttpResponseMessage( HttpStatusCode.BadRequest )
{
    Content = new ObjectContent<HttpError>( error, GlobalConfiguration.Configuration.Formatters.JsonFormatter )
};

throw new HttpResponseException( message );
于 2013-10-01T19:37:20.873 回答
0

您必须创建 System.Net.Http.HttpRequestMessage 的新实例。下面会工作

var request = new HttpRequestMessage();
var response = request.CreateErrorResponse(HttpStatusCode.NotFound, "文件大小不正确");

抛出新的 HttpResponseException(响应);
于 2014-09-28T21:56:21.090 回答
0

尼扎里的回答是正确的,但我最终使用了以下内容:

throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content = new StringContent("Invalid image dimensions. Image file must be " + image.Width + "x" + image.Height + "px"),
                        StatusCode = HttpStatusCode.Forbidden
                    });

并使用访问客户端的内容jqXHR.responseText

感谢大家!

于 2013-07-18T05:56:16.580 回答
-1
HttpResponseMessage message = new  HttpResponseMessage(HttpStatusCode.InternalServerError); 
message.Content = new ObjectContent(typeof(MessageResponse),
                                    "Invalid Size",
                                    GlobalConfiguration.Configuration.Formatters.JsonFormatter); 
throw new HttpResponseException(message);
于 2015-12-20T10:31:12.360 回答