调用获取请求web api时出现奇怪问题
本文关键字:问题 获取 请求 web api 调用 | 更新日期: 2023-09-27 18:27:52
我有一个ASP.NET WebAPI项目,该项目定义了Getneneneba API-"约会/可用",如下所示:
using System;
using System.Web.Http;
using AppointmentScheduler;
using Microsoft.Practices.Unity;
[RoutePrefix("appointment")]
public class AppointmentController : ApiController
{
#region [ Properties ]
[Dependency]
public IAppointmentScheduler AppointmentScheduler { get; set; }
#endregion
#region [ Methods - API ]
[Route("available")]
[HttpGet]
public IHttpActionResult GetAvailableAppointments(string agentEmailId, DateTime startDate, DateTime endDate) {
try {
return base.Ok(this.AppointmentScheduler.GetAppointments(agentEmailId, startDate, endDate));
} catch (Exception exception) {
return base.InternalServerError(exception);
}
}
#endregion
}
然而,在运行解决方案并从Fiddler调用API作为"GET"请求时,我得到以下错误:
HTTP 405错误-请求的资源不支持HTTP方法"GET"。
在发送与"POST"相同的请求时,我得到HTTP-200响应。尽管我的方法从未被调用(API方法上的断点从未被调用)
我在这里查看了以前关于这个问题的查询。但似乎没有一个答案能解决我的问题。
有趣的是,除非我疯了,否则确切的代码在某个时候对我有效。
我认为当我试图在VS 2015上打开最初建立在VS 2013上的解决方案时,这个问题就开始出现了。我觉得,只有在那之后,解决方案才开始在两个IDE中出现问题。
原来我调用了一个带有错误url的API-我没有调用"appointment/available",而是错误地调用了"API/appointment/aavailable"。结果在一个无关紧要的问题上浪费了我一上午的时间。
然而,有一件事我仍然无法理解,为什么当API不存在时,它为"Get"请求返回Http状态405,为"Post"返回200,而不是"404"。
更新:
有问题了,在同一个控制器中还有一个方法。
[HttpPost]
public IHttpActionResult BlockAppointment(Appointment appointment) {
try {
return base.Ok(this.AppointmentScheduler.BlockAppointment(appointment));
} catch (Exception exception) {
return base.InternalServerError(exception);
}
}
对上述方法的这个API调用是:POSTAPI/预约。所以,我实际上调用了上面的方法,而不是Fiddler中的缩进方法,但从未意识到这一点。