"Method not found:" System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request () ".

short question. Here is my controller:

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; } } } 

... here is my 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; } } } 

... and here is my POST request:

Postman post request

... as you can see in the screenshot, I am sending DTO in JSON to the POST method in the API controller. I just can't find a solution. DELETE and GET queries work without problems. This is a training project, so don’t worry about the weird timing methods that I put in the controller ...

+15
source share
2 answers

I found a solution for this.

after I started the assembly, assembly warnings went to the output window, but did not appear in the main error / warning window.

check the output / error window if there are errors or warnings, and then try to solve them.

They were related to assembly conflicts and said they recommend putting assembly redirection in web.Config .

Once I went through them, it all works now.

eg:

  <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> 

Another thing you can try: make your method like

 public IHttpActionResult CreateCustomer([FromBody]CustomerDTO customerDTO){} 

see if that helps ..

+37
source

I have the same problem, but on the other hand. I have MyProject with System.Net.Http library and MyUnitTestProject with System.Net.Http too. Once upon a time I ran unit tests, it crashed with a strange problem. Not final: test not running: Inconclusive: test not run

And where was there some kind of exception "JetBrains came out with code ...." that says nothing to me: Resharper Unit Test exception

Then I found out what the problem was in the redirect binding for System.Net.Http, which was added by some NuGet package. I remove the binding redirection, but get another exception close to your "System.Net.Http.HttpRequestMessage ...": enter image description here

The problem was in the library versions - MyProject had 4.0.0.0 (from the .net Framework), but MyUnitTestProject was 4.2.0.0 (from the Visual Studio folder), and a binding redirect was added for 4.2.0.0, which, I think, cannot be found. So I change it to 4.0.0.0, and it works:

 <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> 
+1
source

Source: https://habr.com/ru/post/1273698/


All Articles