Web request "Request" is zero

I am trying to return an HttpResponseException from a POST Web API action using "Request":

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

In doing so, I get Value cannot be null. Parameter name: request Value cannot be null. Parameter name: request .

Essentially - The request is null.

What am I missing?

thanks

+4
source share
7 answers

Use this:

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

Note. You can change the "Invalid Size" to any object that you might want to return to the user. eg:

 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
source

I found that my Request object was null in my ApiController because I nested one call of ApiController in another. Therefore, the second, nested ApiController was never initialized. You cannot initialize it manually, but you can use the setter of the Request object to pass from the shell to the nested ApiController. The layout below, in my real code, he fixed the Request.CreateErrorResponse (...) error.

 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")); } } 
+5
source

Another solution for people like @ DavB.cs who have nested ApiControllers:

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

Just simply pass the request from the ParentController as such:

 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(...); } } 

The request comes from ApiController. This will lead to the "Request Invalid" error.

Hope this helps someone.

+2
source

The answer to nizari is correct, however I ended up using the following:

 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 }); 

and accessing client-side content using jqXHR.responseText

Thanks everyone!

0
source

Extension of the answer to nizari. Here is what I did to include a list of ErrorMessage objects:

 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 ); 
0
source

you need to create a new instance of System.Net.Http.HttpRequestMessage. below will work

  var request = new HttpRequestMessage ();
 var response = request.CreateErrorResponse (HttpStatusCode.NotFound, "File not in a correct size");

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

Source: https://habr.com/ru/post/1491896/


All Articles