模型状态有效,但返回状态 400
本文关键字:状态 返回 有效 模型 | 更新日期: 2023-09-27 18:32:23
>我试图弄清楚我的路由发生了什么。下面是控制器中具有两条路由的操作。
public enum AvaibleScheduleEventParticipantGroupType
{
Teachers,
...
}
[HttpGet]
[Route("groups/{type}/{id?}", Order = 1)]
[Route("groups/{type}", Order = 2)]
[ResponseType(typeof(IEnumerable))]
public IEnumerable GetParticipantGroupForScheduleEvent(AvaibleScheduleEventParticipantGroupType type, Guid? id = null)
{
var request = new ScheduleEventParticipantGroupRequest
{
Type = type,
Id = id
};
return GettingParticipantGroupForScheduleEventService.HandleGroupRequest(request);
}
在花了一些时间玩弄路由路径后,我终于找到了一个解决方案,当我尝试导航时,它不会以消息"在控制器上找不到操作"
结束 http://localhost:65358/api/scheduleevents/groups/teachers
现在我变得400 status
了,我没有对我的参数进行验证。奇怪。我用Output
窗口查看了 IIS 中发生的事情,发现这个:
w3wp.exe Information: 0 : Request, Method=GET, Url=http://localhost:65358/api/scheduleevents/groups/teachers, Message='http://localhost:65358/api/scheduleevents/groups/teachers'
w3wp.exe Information: 0 : Message='ScheduleEvent', Operation=DefaultHttpControllerSelector.SelectController
w3wp.exe Information: 0 : Message='Nau.Dzienniczek.Api.Areas.Schedule.Controllers.ScheduleEventController', Operation=DefaultHttpControllerActivator.Create
w3wp.exe Information: 0 : Message='Nau.Dzienniczek.Api.Areas.Schedule.Controllers.ScheduleEventController', Operation=HttpControllerDescriptor.CreateController
w3wp.exe Information: 0 : Message='Selected action 'GetParticipantGroupForScheduleEvent(AvaibleScheduleEventParticipantGroupType type, Nullable`1 id)'', Operation=ApiControllerActionSelector.SelectAction
w3wp.exe Information: 0 : Operation=AuthorizeAttribute.OnAuthorizationAsync
w3wp.exe Information: 0 : Message='Parameter 'type' bound to the value 'Teachers'', Operation=ModelBinderParameterBinding.ExecuteBindingAsync
w3wp.exe Information: 0 : Message='Parameter 'id' bound to the value 'null'', Operation=ModelBinderParameterBinding.ExecuteBindingAsync
w3wp.exe Information: 0 : Message='Model state is valid. Values: type=Teachers, id=null', Operation=HttpActionBinding.ExecuteBindingAsync
w3wp.exe Information: 0 : Operation=ValidateModelAttribute.OnActionExecutingAsync, Status=400 (BadRequest)
w3wp.exe Information: 0 : Operation=ScheduleEventController.ExecuteAsync, Status=400 (BadRequest)
w3wp.exe Information: 0 : Operation=DependencyScopeHandler.SendAsync, Status=400 (BadRequest)
w3wp.exe Information: 0 : Response, Status=400 (BadRequest), Method=GET, Url=http://localhost:65358/api/scheduleevents/groups/teachers, Message='Content-type='none', content-length=unknown'
w3wp.exe Information: 0 : Operation=ScheduleEventController.Dispose
仔细观察底部的第 6 行。
'Model state is valid. Values: type=Teachers, id=null'
其次是
Operation=ValidateModelAttribute.OnActionExecutingAsync, Status=400 (BadRequest)
这到底是怎么回事呢?
下面的网址就像一个魅力。
http://localhost:65358/api/scheduleevents/groups/teachers/1e7cb4f9-8e6a-4127-9505-5fad9978ebc6
我不相信 webAPI 可以处理路由中枚举的转换,所以我会将其更改为字符串。此外,由于 id 参数是可选的,因此您可以压缩路由。
[HttpGet, Route("api/scheduleevents/groups/{type}/{id:guid?}")]
public IHttpActionResult GetParticipantGroupForScheduleEvent(string type, Guid? id = null)
{
try
{
var request = new ScheduleEventParticipantGroupRequest
{
Type = type,
Id = id
};
//assuming this returns an object or list of objects
var response = GettingParticipantGroupForScheduleEventService.HandleGroupRequest(request);
return Ok(response);
}
catch
{
return InternalServerError();
}
}
编辑:我刚刚注意到您正在呼叫的路线是:http://localhost:65358/api/scheduleevents/groups/teachers
除非您在 api/scheduleevents 类上使用 routeT前缀,否则您需要将其添加到路由中。我在上面的答案中添加了它。