【发布时间】:2016-05-07 05:52:56
【问题描述】:
我正试图弄清楚我的路由发生了什么。这是控制器中具有两条路由的操作。
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
【问题讨论】:
标签: c# validation routing asp.net-web-api2