2015-06-24 84 views
0

我正在尝试使用实体框架构建OData端点。路由工作正常。Json参数始终为空

我在我的控制器中有一个post方法,post请求可以正确路由,但是即使我传递参数(使用Fiddler 4)它也会收到null参数。

我控制器的方法是:

[HttpPost,EnableQuery] 
    public IHttpActionResult GetConfig_Post(ODataQueryOptions<GetConfig_Result> options, ODataActionParameters parameter) 
     { ... } 

的ODataActionParameters参数始终是空的。

路由代码:

public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) 
     { 
      if (odataPath != null && odataPath.PathTemplate == "~/entityset/action") 
      { 
       var segment = odataPath.Segments.First() as EntitySetPathSegment; 
       string typeName = segment.EntitySetName; 
       if (controllerContext != null && (segment != null && controllerContext.Request.Method == HttpMethod.Post)) 
       { 
        if (typeName == "Config") 
        { 
         return "GetConfig_Post"; 
        } 
       } 
      } 

我找不到为什么下面的POST请求与空参数处理的原因。 [请求如在提琴手,HTTP版本设置为1.1]

User-Agent: Fiddler 
Content-Type: application/json 
Host: localhost:52673 
Content-Length: 36 

Data: 
{"id":"21"} 

回答

0

由于正在使用的WebAPI和在请求主体相信你必须使用[FromBody]参数属性传递数据。

更重要的是,我相信你只能在控制器的POST动作中使用一个参数。

+0

frombody与您认为提交表单的位置相关。 –

+0

@AmitKumarGhosh - 不正确时,它在绑定来自Ajax POST请求的数据时也被广泛使用。它仍然是HTTP POST。 – Kamo

+0

我在建立WebApi和使用$ .ajax的同时还没有使用这个属性。完美的作品。 –