[英]Why do I get an HTTP 404 on this one Web API request?
I am making a POST
request, and getting a 404 - Not Found
error back, to this controller and action: 我正在发出POST
请求,并向此控制器和操作发回404 - Not Found
错误:
[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;
}
}
The request is made from a WPF client using HttpClient
from the Microsoft.AspNet.WebApi.Client
package. 该请求来自使用Microsoft.AspNet.WebApi.Client
包中的HttpClient
的WPF客户端。 The client is configured like so: 客户端配置如下:
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"));
}
}
And called as follows: 并呼吁如下:
var response = await Client.PostAsJsonAsync("api/Appt", model, cancellationToken);
Properties on response
include: response
属性包括:
StatusCode: 404`
ReasonPhrase: 'Not Found'`
RequestMessage:
{Method: POST, RequestUri: 'http://localhost:51009/api/Appt'`
This is the only request I'm making to a POST
action, with a model
parameter, but, ironically, a POST
request to a GET
action on an almost identical controller works fine. 这是我使用model
参数对POST
操作进行的唯一请求,但具有讽刺意味的是,对几乎相同的控制器上的GET
操作的POST
请求工作正常。 With the controller: 使用控制器:
[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();
}
}
the request made as below works fine and returns all my Person
objects: 如下所示的请求工作正常并返回我的所有Person
对象:
HttpResponseMessage response = await Client.PostAsync("api/Person", null, cancellationToken);
Two similar requests to GET
actions also work perfectly. 对GET
操作的两个类似请求也可以完美地工作。
So why would the one resource be found, and the other not? 那么为什么要找到一个资源,另一个不能找到? Are the any other, hidden reasons a 404 would be returned other than the requested resource not being found? 是否有任何其他隐藏的原因除了找不到所请求的资源之外还会返回404?
Could this be due to a conflict with two types of routing, eg 这可能是由于与两种类型的路由冲突,例如
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I have to use attribute routing in one place, the comment explains why: 我必须在一个地方使用属性路由,注释解释了原因:
[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();
}
You are using the wrong route prefix attribute. 您使用了错误的路由前缀属性。
System.Web.Mvc.RoutePrefix("api/Appt")
is for MVC not Web API. 适用于MVC而非Web API。
You need the one in System.Web.Http
你需要System.Web.Http
那个
System.Web.Http.RoutePrefix("api/Appt")
The reason the person controller example worked is because it defaulted back to convention-based routing. 人员控制器示例工作的原因是因为它违反了基于约定的路由。
original would have worked if you called POST http://localhost:51009/api/Appointment
via convention-based routing. 如果您通过基于约定的路由调用POST http://localhost:51009/api/Appointment
,原来会有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.