OData Patch无法正常工作

时间:2015-10-20 11:07:50

标签: c# odata owin

我有一个ODataController,用于或多或少简单的CRUD功能。 GET,POST和DELETE工作正常,但PATCH始终返回相同的错误:'ODataComplexTypeDeserializer' does not support Read.

这是ModelState Error。我的类和方法如下所示:

[ODataRoutePrefix("Orders")]
public class OrdersController : CommonODataController
{
    ...
    [HttpPatch]
    [ODataRoute("({key})")]
    public async Task<IHttpActionResult> Patch([FromODataUri] Int64 key, Delta<Order> order)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (order == null)
        {
            return BadRequest("Missing order patch information.");
        }
        // do patching stuff
    }
}

因此,在调用PATCH时,ModelState无效且ordernull

由于我已将Order最小化,因此它不仅仅是Order的复杂性,因此它只包含Id。我也尝试过其他实体。

在做了一些研究后,我猜它可能与JsonMediaTypeFormatter有关。 OWIN配置是:

    public void Configuration( IAppBuilder app )
    {
        // create the HttpConfiguration and do not use the GlobalConfiguration with OWIN
        HttpConfiguration httpConfiguration = new HttpConfiguration();
        httpConfiguration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
        app.UseCors( CorsOptions.AllowAll );

        IUnityContainer container = new UnityContainer();

        // setup dependency injection and register interfaces
        RegisterServices(container);
        RegisterControllers( container );

        // configure JSON formatters
        JsonMediaTypeFormatter formatter = httpConfiguration.Formatters.JsonFormatter;
        formatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
        //formatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        formatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

        // register routes
        WebApiConfig.Register(httpConfiguration);          
        ODataConfig.Register( httpConfiguration );

        // setup dependency resolver
        httpConfiguration.DependencyResolver = new UnityDependencyResolver(container);

        app.UseWebApi( httpConfiguration );
    }

有人对此有所了解吗?

1 个答案:

答案 0 :(得分:0)

从错误消息中,您似乎想要修补复杂类型属性。但是,到目前为止,Web API OData中不支持它。看相关问题: https://github.com/OData/WebApi/issues/135

感谢。