Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Commented Issue: "HttpResponseMessage Get(string key)" and "Entity GetEntityByKey(string key)" parse key differently [915]

$
0
0
I have an Entity model that uses a string for the primary key.

```
public class Program
{
public string Id { get; set; }
public string Name { get; set; }
}
```

I build my model using convention:

```
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Program>("Programs");
IEdmModel model = modelBuilder.GetEdmModel();
config.Routes.MapODataRoute("ODataRoute", "odata", model);
```

And when I visit the /odata/Programs address, this is what one of the urls that are automatically generated looks like (a picture of the "odata=verbose" response from the controller is attached). Notice the single quotes around the key:

```
http://localhost:4730/odata/Programs('6d6e6d74-2410-4463-997d-d28f03145139')
```

That worked fine when I was only using the "OData" methods on the EntitySetController, such as:

```
Program GetEntityByKey(string key)
Program UpdateEntity(string key, Program update)
```

The problem came when I started needing to return HttpStatusCodes. I saw EntitySetController had the following method I could override, just like the vanilla ApiController:

```
HttpResponseMessage Get(string key)
```

But when I overrode it, I noticed the API no longer worked like I expected. The url that's automatically generated contains single quotes in the key (Programs('6d6e6d74-2410-4463-997d-d28f03145139')), and those quotes are stripped out when calling the GetEntityByKey(string key) method. But when the "HttpResponseMessage Get(string key)" method is overridden, the controller calls that method instead (which is what I want), but it _doesn't_ strip out the single quotes. The key that gets passed into the HttpResponseMessage method looks like this:

```
'6d6e6d74-2410-4463-997d-d28f03145139'
```

So in order to un-break the API, I have to check for and remove the single quotes at the beginning of every method that returns an HttpResponseMessage:

```
public override HttpResponseMessage Get(string key)
{
if (key.StartsWith("'")) {
key key = key.Substring(1, key.Length-2);
}
... // do controllers stuff
```

Is this intentional or a bug? If this is intentional, do you have a suggestion on how I should handle this?
Comments: @jchansen: you forgot to put the [FromODataUri] attribute on the 'Get' method. The base class EntitySetController does that for you. When you override that method, you need to put the attribute again as parameter attributes are not inherited in C#. You didn't have to put the [FromODataUri] attribute on GetEntityByKey as it is not a web API action. The real action just calls this helper method. That's why GetEntityByKey doesn't require [FromODataUri]

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>