“找不到方法:'System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()'." [英] "Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()'."

查看:100
本文介绍了“找不到方法:'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;
        }


    }
}

...这是我的DTO:

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

    }
}

...这是我的POST请求:

... and here is my POST request:

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

... as you can see on the screenshot I'm sending DTO in JSON to POST method in API controller. I just can't find solution. DELETE and GET requests are working with no problem. It is a training project so don't worry about those weird temporary mapping methods I have put in controller ...

推荐答案

我找到了解决方案.

我开始构建后,有构建警告会转到输出窗口,但不会显示在主错误/警告窗口中.

after I starting building there was build warnings going to the output window but not showing in the main error / warning window.

检查您的输出/错误窗口是否有错误或警告,然后尝试解决..

check your output/error window if there are errors or warning then try to solve it..

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

They were to do with assembly conflicts and said recommend putting the assembly redirect in the web.Config.

一旦我仔细阅读了所有内容,现在一切正常.

Once I had went through them all it now works.

例如:

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

您可以尝试的其他方法是: 使您的方法像

other thing you can try is : make your method like

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

看看是否有帮助.

这篇关于“找不到方法:'System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()'."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆