Quantcast
Channel: ASP.NET MVC / Web API / Web Pages
Viewing all articles
Browse latest Browse all 7925

Commented Issue: Delete fails if entity key is of type Guid [885]

$
0
0
I send the following ODATA message:

_DELETE http://localhost:34852/Tags(guid'43e361ba-a8a0-4dbe-8474-dfcb5496c161') HTTP/1.1__
DataServiceVersion: 3.0
MaxDataServiceVersion: 3.0
Host: localhost:34852

I get back an error:

HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; odata=minimalmetadata; streaming=true; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
DataServiceVersion: 3.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcY3BtU291cmNlc1xLbm93bGVkZ2UgTWFuYWdlclxEZXZlbG9wbWVudFxEYXRhU2VydmljZVByb3RvdHlwZVxXZWJBcGlEYXRhU2VydmljZVxQaW1zQ29uZmlnRGF0YVN2Y1xBQkIuUElNUy5TZXJ2ZXIuQ29uZmlnU2VydmljZVxUYWdzKGd1aWQnNDNlMzYxYmEtYThhMC00ZGJlLTg0NzQtZGZjYjU0OTZjMTYxJyk=?=
X-Powered-By: ASP.NET
Date: Tue, 05 Mar 2013 11:36:20 GMT
Content-Length: 561

{
"odata.error":{
"code":"","message":{
"lang":"en-US","value":"The request is invalid."
},"innererror":{
"message":"The parameters dictionary contains a null entry for parameter 'key' of non-nullable type 'System.Guid' for method 'Void Delete(System.Guid)' in 'ABB.PIMS.Server.ConfigService.Controllers.Base.StoreEntitySetController`2[ABB.PIMS.Data.Model.Domain.RTDB.Tag,System.Guid]'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.","type":"","stacktrace":""
}
}
}

The error happens before any of the(standard and custom) routing conventions are evaluated.
GetByKey works correctly.

I use the released version of WebApi OData (v4.0.30319). See attachment for relevant trace log part.

Comments: I did some ModelBinder work but then dug in further and now understand a LOT more than I need to about how WebAPI and OData is implemented :) I think my problem was pretty much all my fault in hindsight. I was initially passing malformed GUIDs to my service, and the only way I could make them work was to make my own method on my EntitySetController-derived class of the form public override HttpResponseMessage Get(Guid key) { return base.Get(key); } I knew at the time this was crazy but it worked. That's before I realised I was using malformed GUIDs. Using correctly formatted OData GUIDs with that method in place confused the WebAPI binding infrastructure since the OData-style GUID didn't match what WebAPI itself (sans OData) was expecting. After a lot of tracing through code I've found that the builtin OData model binding code doesn't support malformed GUIDs (with the guid' prefix) as the ODataLib library referenced by WebAPI just turns them into a string-wrapped int. For example the GUID 94abcdxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx turns into "94", which then can't be converted to a GUID. Long (winding) story short: You don't have to override Get(EntityType Key), and if you find you do, then you're "doing it wrong" :) Once not overridden then GUIDs can be properly passed in. One error on my part broke other stuff that seemed unrelated at the time. Bleh - off to have dinner :) Sorry to clutter up the thread.

Viewing all articles
Browse latest Browse all 7925

Trending Articles