OData 4 web api 2 post总是为null

时间:2016-01-08 11:40:55

标签: jquery angularjs post asp.net-web-api odata

每次我将数据发布到我的OData控制器时,我的模型都是空的。 这是我的控制器

public class DraftController : ODataController
{
    public HttpResponseMessage Post([FromBody] DraftApi d)
    {
        try
        {
            return Request.CreateResponse(HttpStatusCode.OK, "(POST) id: " + d.id + ", desc: " + d.description);
        }
        catch (Exception e)
        {
            return Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message);
        }
    }
}

这是我的模特

public class DraftApi
{
    [Key]
    public string id { get; set; }
    public string description { get; set; }
}

这是我在angularjs中的调用(例如),你可以在jquery或其他

中执行此操作
var obj = {
    id: "a",
    description: "abc" 
};
$http({
    method: "POST",
    url: "http:127.0.0.1/mpssapi/odata/v1/draft",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: obj
}).success(function (data, status, headers, config) {
    console.log(data);
}).error(function (data, status, headers, config) {
    alert(data);
});

我的OData路线

config.MapODataServiceRoute(
    routeName: "ODataDraft",
    routePrefix: "odata/{namespace}",
    model: BuildModel<DraftApi>("draft")
);

private static IEdmModel BuildModel<T>(string EntityName) where T : class
{
    ODataConventionModelBuilder ODataBuilder = new ODataConventionModelBuilder();
    ODataBuilder.EntitySet<T>(EntityName).EntityType.Name = EntityName;
    ODataBuilder.EnableLowerCamelCase();
    return ODataBuilder.GetEdmModel();
}

总是我的回答是

{
"@odata.context":"http://127.0.0.1/mpssapi/odata/v1/$metadata#Edm.String","value":"(POST) id: , desc: "
}

任何想法? 感谢

0 个答案:

没有答案