私はPOST
要求を行うと、このコントローラとアクションに、バック404 - Not Found
エラーを取得しています:この1つのWeb 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
パラメータを使用しますが、皮肉なことに、ほぼ同じコントロールのGET
アクションへのPOST
リクエストlerはうまく動作します。コントローラでは:
[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();
}
}
以下のように行われた要求が正常に動作し、すべての私のPerson
オブジェクトを返します。
HttpResponseMessage response = await Client.PostAsync("api/Person", null, cancellationToken);
二GET
アクションに似要求も完璧に動作します。
なぜ、1つのリソースが見つかるのでしょうか?要求されたリソース以外が見つからない他の隠し理由が返されますか?
これは、2つのタイプのルーティングと競合している可能性があります。私は1つの場所でのルーティング属性を使用する必要が
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
、コメントは理由を説明:
[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();
}