“找不到方法:‘System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()’。”

2024-04-13

这是我的控制器:

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

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

我的 POST 请求:

正如您在屏幕截图中看到的,我将 JSON 格式的 DTO 发送到 API 控制器中的 POST 方法。我只是找不到解决办法。 DELETE 和 GET 请求工作正常。这是一个培训项目,所以不要担心我在控制器中放入的那些奇怪的临时映射方法。


我找到了解决这个问题的方法。

开始构建后,输出窗口中出现构建警告,但未显示在主错误/警告窗口中。

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

他们与程序集冲突有关,并表示建议将程序集重定向放在网络配置.

一旦我完成了所有这些,它现在就可以工作了。

例如:

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

您可以尝试的其他事情是: 让你的方法像

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

看看这是否有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

“找不到方法:‘System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()’。” 的相关文章

随机推荐