Post &引用;未找到方法:';System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get#u Request()'&引用;

Post &引用;未找到方法:';System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get#u Request()'&引用;,post,asp.net-web-api,Post,Asp.net Web Api,这是我的控制器: 使用系统; 使用System.Collections.Generic; 使用System.Linq; Net系统; 使用System.Net.Http; 使用System.Web.Http; 使用Milos_MovieStore.DAL; 使用Milos_MovieStore.Models; 使用Milos_MovieStore.DTO; 使用System.Data.Entity; 名称空间Milos_MovieStore.Controllers.Api { 公共类Custom

这是我的控制器:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Net.Http;
使用System.Web.Http;
使用Milos_MovieStore.DAL;
使用Milos_MovieStore.Models;
使用Milos_MovieStore.DTO;
使用System.Data.Entity;
名称空间Milos_MovieStore.Controllers.Api
{
公共类CustomerController:ApicController
{
私有DBContext_MovieStore_DBContext;
公共CustomerController()
{
_dbcontext=newdbcontext_MovieStore();
}
受保护的覆盖无效处置(布尔处置)
{
_dbcontext.Dispose();
}
//获取/api/客户
[HttpGet]
公共IHttpActionResult GetCustomers()
{
List customers=_dbcontext.customers.Include(c=>c.MembershipType.ToList();
返回Ok(CustomersToList(customers));
}
//GET/api/customers/1
[HttpGet]
公共IHttpActionResult GetCustomer(内部id)
{
Customer=\u dbcontext.Customers.Include(c=>c.MembershipType).SingleOrDefault(c=>c.Id==Id);
如果(客户==null)
返回NotFound();
返回Ok(CustomerToDTO(customer));
}
//POST/api/客户
[HttpPost]
公共IHttpActionResult CreateCustomer(CustomerTo CustomerTo)
{
如果(!ModelState.IsValid)
返回请求();
_添加(dtotocuster(customerDTO));
_dbcontext.SaveChanges();
创建的返回(新Uri(Request.RequestUri+“/”+customerDTO.Id),customerDTO);
}
//PUT/api/customer/1
[HttpPut]
公共IHttpActionResult UpdateCustomer(CustomerDTO CustomerDTO)
{
如果(!ModelState.IsValid)
返回请求();
CustomerCustomerInDB=\u dbcontext.Customers.SingleOrDefault(c=>c.Id==customerDTO.Id);
if(customerInDB==null)
返回NotFound();
MapDTOToCustomer(CustomerTo、customerInDB);
_dbcontext.SaveChanges();
返回Ok(customerDTO);
}
//删除/api/customer/1
[HttpDelete]
公共IHttpActionResult DeleteCustomer(内部id)
{
如果(!ModelState.IsValid)
返回请求();
CustomerCustomerInDB=\u dbcontext.Customers.SingleOrDefault(c=>c.Id==Id);
if(customerInDB==null)
返回NotFound();
_dbcontext.Customers.Remove(customerInDB);
_dbcontext.SaveChanges();
返回Ok(id);
}
私人客户To CustomerTo(客户客户)
{
CustomerDTO CustomerDTO=新CustomerDTO();
customerDTO.Id=customer.Id;
customerDTO.Name=customer.Name;
customerDTO.DOB=customer.DOB;
customerDTO.MembershipTypeId=customer.MembershipTypeId;
customerDTO.IsSubscribedToNewsletter=customer.IsSubscribedToNewsletter;
退回客户;
}
私人客户DTOToCustomer(CustomerTo CustomerTo)
{
客户=新客户();
customer.Id=customerDTO.Id;
customer.Name=customerDTO.Name;
customer.DOB=customerDTO.DOB;
customer.MembershipTypeId=customerDTO.MembershipTypeId;
customer.IsSubscribedToNewsletter=customerDTO.IsSubscribedToNewsletter;
退货客户;
}
私有void MapdToCustomer(CustomerTo CustomerTo,Customer-Customer)
{
customer.Id=customerDTO.Id;
customer.Name=customerDTO.Name;
customer.DOB=customerDTO.DOB;
customer.MembershipTypeId=customerDTO.MembershipTypeId;
customer.IsSubscribedToNewsletter=customerDTO.IsSubscribedToNewsletter;
}
私有IEnumerable客户TodToList(IEnumerable客户)
{
List customersDTO=新列表();
foreach(客户中的客户c)
{
customersDTO.Add(CustomerToDTO(c));
}
将客户退回至;
}
}
}
我的DTO:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.ComponentModel.DataAnnotations;
名称空间Milos_MovieStore.DTO
{
公共类CustomerDTO
{
公共int Id{get;set;}
[必需]
[StringLength(255)]
公共字符串名称{get;set;}
公共日期时间?DOB{get;set;}
公共字节成员类型ID{get;set;}
公共bool IsSubscribedToNewsletter{get;set;}
}
}
我的发帖要求:


正如您在屏幕截图中看到的,我正在将JSON中的DTO发送到API控制器中的POST方法。我就是找不到解决办法。删除和获取请求可以正常工作。这是一个培训项目,所以不要担心我在controller中使用的那些奇怪的临时映射方法。

我找到了解决方案

在我开始构建之后,有构建警告进入输出窗口,但没有显示在主错误/警告窗口中

如果有错误或警告,请检查输出/错误窗口,然后尝试解决它

他们与程序集冲突有关,并建议将程序集重定向放在web.Config

一旦我把它们都看了一遍,它现在就起作用了

例如:


您可以尝试的另一件事是: 让你的方法像

public IHttpActionResult CreateCustomer([FromBody]CustomerDTO CustomerDTO){
看看这是否有帮助。

我也有同样的问题b