我正在尝试使用“请求”从POST Web API操作返回HttpResponseException
:
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "File not in a correct size"));
这样做时,我得到Value cannot be null. Parameter name: request
。
基本上 - 请求为空。
我错过了什么?
谢谢
答案 0 :(得分:5)
我在我的ApiController中发现我的Request对象为null,因为我在另一个中嵌套了一个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"));
}
}
答案 1 :(得分: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);
答案 2 :(得分:2)
像@ DavB.cs这样嵌套ApiControllers的人的另一种解决方案,如:
public class ParentController : ApiController
{
public IHttpActionResult Foo()
{
ChildRepository.Foo();
return Ok(...);
}
}
public class ChildRepository : ApiController
{
public HttpResponseMessage Foo()
{
// Do something
return Request.CreateResponse(...);
}
}
只是简单地从ParentController传递Request:
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。这将使您超越“请求为空”问题。
希望这有助于某人。
答案 3 :(得分:0)
nizari的回答是正确的,但我最终使用了以下内容:
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
谢谢大家!
答案 4 :(得分: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 );
答案 5 :(得分:0)
您必须创建System.Net.Http.HttpRequestMessage的新实例。以下将起作用
var request = new HttpRequestMessage(); var response = request.CreateErrorResponse(HttpStatusCode.NotFound, "File not in a correct size"); throw new HttpResponseException(response);
答案 6 :(得分:-1)
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.InternalServerError);
message.Content = new ObjectContent(typeof(MessageResponse),
"Invalid Size",
GlobalConfiguration.Configuration.Formatters.JsonFormatter);
throw new HttpResponseException(message);