Manju
Manju

Reputation: 2650

ASP.NET WebAPI error on sending response

I'm trying to send the response from one of my POST methods of my webAPI controller. In the DB the values are getting saved, but while sending the response in both try and in catch it is throwing the following exception.

Exception Message: "Value cannot be null. Parameter name: request."

Controller method code:

[HttpPost]
    public HttpResponseMessage AddEmployee(Employee emp)
    {
        try
        {
            using (EmployeeEntities dbEntity = new EmployeeEntities())
            {
                dbEntity.Configuration.LazyLoadingEnabled = false;
                dbEntity.Employees.Add(emp);
                dbEntity.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.Created, emp); // EXCEPTION IS THROWN HERE
            }
        }
        catch(Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
        }
    }

enter image description here

Please let me know what i need to do to resolve this issue. Thanks.

Calling the API controller method from a MVC controller method as below,

EmployeeController empCtrl = new EmployeeController();
empCtrl.AddEmployee(emp);

Upvotes: 1

Views: 718

Answers (2)

DaveG.
DaveG.

Reputation: 31

The actual extension method CreateResponse is complaining about getting a null reference. Nkosi's answer is technically correct but to prevent a lot of refactoring, just change your controller registration so that the controller you're using here is directly responding to the request (like it seems you intend from the code given here)

Upvotes: 0

Nkosi
Nkosi

Reputation: 247531

You are manually creating the controller. The framework normally creates that as part of the request flow. By manually creating it you are not populating other required properties like the Request which is why your Request is null.

Extract what you want to do out of the Web API into a service and inject that into both the MVC and Web API controller. That way the MVC controller has no need to be creating the API controller.

Some simple examples

public EmployeeService : IEmployeeService {
    public Employee AddEmployee(Employee emp) {
        using (EmployeeEntities dbEntity = new EmployeeEntities()) {
            dbEntity.Configuration.LazyLoadingEnabled = false;
            dbEntity.Employees.Add(emp);
            dbEntity.SaveChanges();
            return emp;
        }
    }

    //...other service members
}

The web API will use the service

public class EmployeeController : ApiController {
    private readonly IEmployeeService employeeService;

    public EmployeeController(IEmployeeService service) {
        this.employeeService = service;
    }

    [HttpPost]
    public HttpResponseMessage AddEmployee(Employee emp) {
        try {
            employeeService.AddEmployee(emp);
            return Request.CreateResponse(HttpStatusCode.Created, emp);
        } catch(Exception ex) {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
        }
    }

    //...other actions.
}

And the MVC Controller can use the same service as well.

Upvotes: 2

Related Questions