cem*_*mbo 22 post asp.net-web-api
简短的问题.这是我的控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Milos_MovieStore.DAL;
using Milos_MovieStore.Models;
using Milos_MovieStore.DTO;
using System.Data.Entity;
namespace Milos_MovieStore.Controllers.Api
{
public class CustomersController : ApiController
{
private DBContext_MovieStore _dbcontext;
public CustomersController()
{
_dbcontext = new DBContext_MovieStore();
}
protected override void Dispose(bool disposing)
{
_dbcontext.Dispose();
}
// GET /api/customers
[HttpGet]
public IHttpActionResult GetCustomers()
{
List<Customer> customers = _dbcontext.Customers.Include(c => c.MembershipType).ToList();
return Ok(CustomersToDTOList(customers));
}
// GET /api/customers/1
[HttpGet]
public IHttpActionResult GetCustomer(int id)
{
Customer customer = _dbcontext.Customers.Include(c => c.MembershipType).SingleOrDefault(c => c.Id == id);
if (customer == null)
return NotFound();
return Ok(CustomerToDTO(customer));
}
//POST /api/customers
[HttpPost]
public IHttpActionResult CreateCustomer(CustomerDTO customerDTO)
{
if (!ModelState.IsValid)
return BadRequest();
_dbcontext.Customers.Add(DTOToCustomer(customerDTO));
_dbcontext.SaveChanges();
return Created(new Uri(Request.RequestUri + "/" + customerDTO.Id), customerDTO);
}
// PUT /api/customer/1
[HttpPut]
public IHttpActionResult UpdateCustomer(CustomerDTO customerDTO)
{
if (!ModelState.IsValid)
return BadRequest();
Customer customerInDB = _dbcontext.Customers.SingleOrDefault(c => c.Id == customerDTO.Id);
if (customerInDB == null)
return NotFound();
MapDTOToCustomer(customerDTO, customerInDB);
_dbcontext.SaveChanges();
return Ok(customerDTO);
}
// DELETE /api/customer/1
[HttpDelete]
public IHttpActionResult DeleteCustomer(int id)
{
if (!ModelState.IsValid)
return BadRequest();
Customer customerInDB = _dbcontext.Customers.SingleOrDefault(c => c.Id == id);
if (customerInDB == null)
return NotFound();
_dbcontext.Customers.Remove(customerInDB);
_dbcontext.SaveChanges();
return Ok(id);
}
private CustomerDTO CustomerToDTO(Customer customer)
{
CustomerDTO customerDTO = new CustomerDTO();
customerDTO.Id = customer.Id;
customerDTO.Name = customer.Name;
customerDTO.DOB = customer.DOB;
customerDTO.MembershipTypeId = customer.MembershipTypeId;
customerDTO.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter;
return customerDTO;
}
private Customer DTOToCustomer(CustomerDTO customerDTO)
{
Customer customer = new Customer();
customer.Id = customerDTO.Id;
customer.Name = customerDTO.Name;
customer.DOB = customerDTO.DOB;
customer.MembershipTypeId = customerDTO.MembershipTypeId;
customer.IsSubscribedToNewsletter = customerDTO.IsSubscribedToNewsletter;
return customer;
}
private void MapDTOToCustomer(CustomerDTO customerDTO, Customer customer)
{
customer.Id = customerDTO.Id;
customer.Name = customerDTO.Name;
customer.DOB = customerDTO.DOB;
customer.MembershipTypeId = customerDTO.MembershipTypeId;
customer.IsSubscribedToNewsletter = customerDTO.IsSubscribedToNewsletter;
}
private IEnumerable<CustomerDTO> CustomersToDTOList(IEnumerable<Customer> customers)
{
List<CustomerDTO> customersDTO = new List<CustomerDTO>();
foreach (Customer c in customers)
{
customersDTO.Add(CustomerToDTO(c));
}
return customersDTO;
}
}
}
Run Code Online (Sandbox Code Playgroud)
......这是我的DTO:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Milos_MovieStore.DTO
{
public class CustomerDTO
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
public DateTime? DOB { get; set; }
public byte MembershipTypeId { get; set; }
public bool IsSubscribedToNewsletter { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
...这是我的POST请求:
...正如您在屏幕截图中看到的那样,我正在将JTO中的DTO发送到API控制器中的POST方法.我只是找不到解决方案.DELETE和GET请求没有问题.这是一个培训项目,所以不要担心我在控制器中放置的那些奇怪的临时映射方法......
Sau*_*nki 56
我找到了解决方案.
在我开始构建之后,构建警告将进入输出窗口但未显示在主错误/警告窗口中.
检查输出/错误窗口,如果有错误或警告,然后尝试解决它..
他们与汇编冲突有关,并建议将汇编重定向放在web.Config中.
一旦我完成了所有这一切,它现在有效.
例如 :
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)
你可以尝试的另一件事是:让你的方法像
public IHttpActionResult CreateCustomer([FromBody]CustomerDTO customerDTO){}
Run Code Online (Sandbox Code Playgroud)
看看这有帮助..
我有同样的问题,但来自不同的方面。我也有带有 System.Net.Http 库的MyProject和带有 System.Net.Http 的MyUnitTestProject。曾几何时,我开始了单元测试,它因一个奇怪的问题而崩溃。不确定:测试未运行:

哪里有一些例外“JetBrains 退出了代码......”,这对我来说没有任何意义:

然后我发现问题出在 System.Net.Http 的绑定重定向中,它是由一些 NuGet 包添加的。我删除了绑定重定向,但获得了另一个异常,接近您的“System.Net.Http.HttpRequestMessage...”:

问题出在库的版本中 - 在MyProject 中是 4.0.0.0(来自 .net Framework),但MyUnitTestProject是 4.2.0.0(来自 Visual Studio 文件夹),并且为 4.2.0.0 添加了绑定重定向,我认为它以某种方式不能成立。所以我将其更改为 4.0.0.0 并且它起作用了:
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)