我正在构建一个简单的 Web API 控制器来管理公司实体。我试图通过使用 Ninject 将所有逻辑移到服务类(依次调用 repo)来管理依赖关系,从而将所有逻辑从控制器中移开。一切都很好,直到我需要在 Delete 操作上返回一个 HttpResponseMessage 对象。我不断收到请求对象“无法解析符号‘请求’”的错误
如何在不是控制器的文件中使用(或获取上下文?)请求对象?
公司控制器.cs
public class CompanyController : ApiController
{
private ICompanyService _companyService;
public CompanyController(ICompanyService companyService)
{
_companyService = companyService;
}
// DELETE api/CompTest/5
public HttpResponseMessage DeleteCompany(int id)
{
return _companyService.Delete(id);
}
}
公司服务.cs
public class CompanyService : ICompanyService
{
public readonly IUnitOfWork _unitOfWork ;
private readonly ICompanyRepository _companyRepository;
public CompanyService(IUnitOfWork unitOfWork, ICompanyRepository companyRepository)
{
_unitOfWork = unitOfWork;
_companyRepository = companyRepository;
}
public HttpResponseMessage Delete(int id)
{
var company = _companyRepository.GetCompanyById(id);
if (company == null)
{
//ERROR HERE
return Request.CreateResponse(HttpStatusCode.NotFound);
}
_companyRepository.Delete(company);
try
{
_companyRepository.Save();
}
catch (DbUpdateConcurrencyException ex)
{
//ERROR HERE
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
//ERROR HERE
return Request.CreateResponse(HttpStatusCode.OK, company);
}
}