简体   繁体   English

发送响应时出现ASP.NET WebAPI错误

[英]ASP.NET WebAPI error on sending response

I'm trying to send the response from one of my POST methods of my webAPI controller. 我正在尝试从webAPI控制器的POST方法之一发送响应。 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. 在DB中,值已保存,但是在try和catch中发送响应时,将引发以下异常。

Exception Message: "Value cannot be null. Parameter name: request." 异常消息:“值不能为null。参数名称: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);
        }
    }

在此处输入图片说明

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, 从MVC控制器方法调用API控制器方法,如下所示:

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

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 . 通过手动创建它,您不会填充其他必需的属性,例如Request ,这就是您的Requestnull

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. 从Web API中提取要执行的操作到服务中,然后将其注入到MVC和Web API控制器中。 That way the MVC controller has no need to be creating the API controller. 这样,MVC控制器无需创建API控制器。

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 Web API将使用该服务

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. MVC控制器也可以使用相同的服务。

The actual extension method CreateResponse is complaining about getting a null reference. 实际的扩展方法CreateResponse抱怨获取空引用。 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) Nkosi的回答在技术上是正确的,但是为了防止大量重构,只需更改您的控制器注册,以便您在此处使用的控制器可以直接响应请求(就像您打算从此处提供的代码中获得意图一样)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM