【问题标题】:Model state is valid, but status 400 is returned模型状态有效,但返回状态 400
【发布时间】: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


    【解决方案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,否则您需要将其添加到您的路由中。我在上面的答案中添加了它。

    【讨论】:

    • 按照您的建议,我将类型更改为字符串,但我仍然收到400
    • 尝试更改路线。我认为错误的请求是由于您在同一端点上有 2 个匹配的路由。还要注意返回类型。
    • 是的,我也做过,只是忘了说:)
    • 在您的 Startup.cs 文件中,您是否在配置方法中有这一行以允许属性路由? configuration.MapHttpAttributeRoutes();
    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2019-09-18
    • 2015-12-08
    • 1970-01-01
    相关资源
    最近更新 更多