Get BadRequestObjectResult on calling a controllers post method in ServerMethodController.cs

I have a method in ServerMethodsController.cs that execute Post method of a controller.

CmdDetailsController cdc = new CmdDetailsController(this.MyData);
IActionResult cdc_ar = cdc.Post(cmd);

I always get BadRequestObjectResult ("Object reference not set to an instance of an object.") for the result but the data was recorded in database.
And when I trace it to CmdDetailsController's Post method

public IActionResult Post([FromBody] Models.MyData.CmdDetail item)

        Request.QueryString = Request.QueryString.Add("$expand", "Change");

        return new ObjectResult(SingleResult.Create(itemToReturn))
            StatusCode = 201
    catch(Exception ex)
        ModelState.AddModelError("", ex.Message);
        return BadRequest(ModelState);

It's the Request.QueryString line that triggered the exception.

I know the Request object is null because I'm not calling the method via http.

How to directly execute a controllers Post method (or any method) without triggering the null exception in Radzen?


Hey @KuKuDaPe,

This is not a proper way to execute controller method - it should be called client-side from your Angular application.

Yes, I realized that much. So I just surround the "Request" line with try and add the controller to "Code generation ignore list" ?

I just copied a few lines of Post method to ServerMethodController. Thanks for your reply.

Why not inject the datacontext via the ServerMethodsController constructor? You can then use Entity framework commands direct?

Yup, I just did that.