当我试图得到一些数据与网页API的参数(类型DateTime
),我得到响应,出现错误404:没有HTTP资源发现,请求URI匹配 - 网页API +角
消息 : “找不到与请求URI相匹配的HTTP资源'http://localhost:53544/api/reservations/GetReservationByDate?date=Tue 2017年1月31日16:00:00 GMT + 0100'。” MessageDetail : “不提供控制器名称找到路由匹配请求的URI 'http://localhost:53544/api/reservations/GetReservationByDate?date=Tue 2017年1月31日16:00:00 GMT + 0100'”
我使用AngularJS这一点。在其他控制器中,我有参数的方法,但都没有DateTime。这可能是问题吗?这之前,我有错误
400错误的请求
和配置路由帮助我。
这是我的代码 - 在控制器方法:
[HttpGet]
[ActionName("GetReservationByDate")]
[ResponseType(typeof(Reservation))]
public IHttpActionResult GetReservationByDate(DateTime date)
{
var reservation = db.Reservations.Where(d => d.Date.Equals(date));
if (reservation == null)
{
return NotFound();
}
return Ok(reservation);
}
RouteConfig.cs:
config.Routes.MapHttpRoute(
name: "CustomApi",
routeTemplate: "api/Reservations/GetReservationByDate/{date}",
defaults: new { date = RouteParameter.Optional}
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
从AngularJS
和方法:
this.GetReservation = function (date) {
var promise = $http({
method: 'GET',
url: 'api/reservations/GetReservationByDate?date=' + date,
config: {
params: {
"date": date
}
}
})
.then(function (response) {
return response;
},
function (response) {
return response;
});
return promise;
}
我尝试了不同的组合,与FromUri
,FromBody
,但结果总是相同的。
我改变它像发送这个,但然后显示错误400错误的请求: 消息 : “该请求无效。” MessageDetail : “参数字典中的ServiceSchedule.Controllers中的方法'System.Web.Http.IHttpActionResult GetReservationByDate(System.DateTime)'中的非可为空类型'System.DateTime'的参数'date' .ReservationsController'。可选参数必须是引用类型,可为空类型,或者声明为可选参数。“ – ryckoshet
对。我知道这意味着什么。因为date参数是可选的,所以它意味着可能没有提供日期(因此为空),但由于它是DateTime,因此操作中的参数不可空。按照异常消息中的说明“可选参数必须是引用类型,可为空类型,或者声明为可选参数。” – Nkosi
解决方案是什么?更改参数的类型..? – ryckoshet