짧은 질문. 여기 내 컨트롤러 :로 ..."메서드를 찾을 수 없습니다 : 'System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()'."
: 여기 내 POST 요청한다
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; }
}
}
... 그리고 :
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이다 당신은 스크린 샷에서 JSON에서 DTO를 API 컨트롤러의 POST 메서드로 보내고 있습니다. 솔루션을 찾을 수 없습니다. DELETE 및 GET 요청은 문제없이 작동합니다. 그것은 훈련 프로젝트이므로 컨트롤러에 넣은 그 이상한 임시 매핑 방법에 대해 걱정하지 마십시오.
(새 열린 우리당 (Request.RequestUri + "/"+ customerDTO.Id), customerDTO); ? Created() 메소드 란 무엇입니까? – toha
... 200 OK 대신에 201 "created"HTTP 응답을 생성하기위한 System.Web.Http의 가상 메소드입니다. Uri의 인수는 문자열 f.e.:"/api/customers "... – cembo