은 내가 POST
요청을하고,이 컨트롤러와 액션에, 다시 404 - Not Found
오류가 점점 오전 :이 웹 API 요청에 HTTP 404가 나타나는 이유는 무엇입니까?
[AllowAnonymous]
[System.Web.Mvc.RoutePrefix("api/Appt")]
public class AppointmentController : BaseController
{
[HttpPost]
[Route("")]
public AppointmentDto Post(AppointmentDto model)
{
Db.Appointments.Add(model);
Db.SaveChanges();
Logger.Info($"Appointment ID {model.Id} created.");
return model;
}
}
요청은 Microsoft.AspNet.WebApi.Client
패키지에서 HttpClient
를 사용하여 WPF 클라이언트에서 이루어집니다. 다음과 같이
public abstract class BaseRestClient
{
protected const string BaseAddress = "http://localhost:51009";
protected HttpClient Client;
protected virtual void ConfigureClient()
{
Client = new HttpClient { BaseAddress = new Uri(BaseAddress) };
Client.DefaultRequestHeaders.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
}
그리고 전화 : response
에
var response = await Client.PostAsJsonAsync("api/Appt", model, cancellationToken);
속성은 다음과 같습니다
이StatusCode: 404`
ReasonPhrase: 'Not Found'`
RequestMessage:
{Method: POST, RequestUri: 'http://localhost:51009/api/Appt'`
이 나는 POST
로 만들고있어 유일한 요청하는 클라이언트는과 같이 구성되어 있습니다 액션, model
매개 변수와 함께하지만, 아이러니하게도 POST
요청은 GET
액션과 거의 동일한 컨트롤 ler는 잘 작동합니다. 아래 만들어진 요청이 잘 작동하고 내 모든 Person
객체를 반환
[System.Web.Mvc.RoutePrefix("api/Person")]
public class PersonController : BaseController
{
[HttpPost]
public async Task<IEnumerable<PersonDto>> Get()
{
Db.Configuration.LazyLoadingEnabled = false;
return await Db.Persons.ToListAsync();
}
}
: 컨트롤러와 GET
행동
HttpResponseMessage response = await Client.PostAsync("api/Person", null, cancellationToken);
두 개의 유사한 요청도 완벽하게 작동합니다.
그럼 왜 한 자원이 발견되고 다른 자원은 발견되지 않습니까? 요청한 리소스가 발견되지 않는 것 이외의 다른 숨겨진 이유 404가 반환됩니까?
두 가지 유형의 라우팅과 충돌 할 수 있습니다 (예 : 당신은 잘못된 경로 접두사 속성을 사용하는
[HttpPost]
[Route("Get")]
// NOTE HTTP 405 - Method not allowed when this action is named 'Get'.
public async Task<IEnumerable<BranchDto>> Fetch()
{
Db.Configuration.LazyLoadingEnabled = false;
return await Db.Branches.ToListAsync();
}