我试图弄清楚我的路由会发生什么。这是控制器中有两条路线的动作。
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)
那是怎么回事?
下面的Url就像一个魅力。
http://localhost:65358/api/scheduleevents/groups/teachers/1e7cb4f9-8e6a-4127-9505-5fad9978ebc6
答案 0 :(得分:1)
我不相信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类上使用routePrefix,否则需要将其添加到路线中。我在上面的答案中添加了它。