Get key from Uri in CreateRef (.NET Core 6 OData)
Asked Answered
I

1

2

While upgrading to OData 8, could not find a way to fetch key from the URI for navigation property.

I'm using preview package of Asp.Versioning.OData and following this guide from Microsoft to fetch key from Uri. This guid has a method Helpers.GetKeyFromUri() but with OData v8, it has many extension methods missing such as GetUrlHelper() or types like KeyValuePathSegment.

Is there a new way to extract key from Uri when using Asp.Versioinng.OData with OData 8 and .net core 6?

I also referred to its source with examples but it did not implement how to fetch the key from Uri.

Isotone answered 31/5, 2022 at 22:18 Comment(6)
It looks like this may already be answered? https://mcmap.net/q/2036562/-odata-obsolete-codeDaberath
@RobG Its similar to guide link I added from Microsoft but unfortunately it does not work with OData 8Isotone
Which lines give you errors from that other answer's sample code? There's some comments on the answer saying there have been more breaking API changes since then, and specifies how to work around that.Daberath
If I use example from your link, there are errors for GetPathHandler(), GetUrlHelper(), GetRequestContainer() and field ODataFeature().RouteNameIsotone
can this blog help you?Filigreed
Thanks but unfortunately this blog post does not talk about creating entity relations through odata. I did refer it earlier though while reading about upgrading to OData 8Isotone
D
3

@soccer7, the guide you are referring to is for OData with ASP.NET Web API (2.2). That does not apply to OData with ASP.NET Core.

The OData documentation doesn't appear to have been updated with how to extract the key. There may be other ways, but the following will definitely work:

[HttpPut]
public IActionResult CreateRef(
    int key,
    string navigationProperty,
    [FromBody] Uri link )
{
    var feature = HttpContext.ODataFeature();
    var model = feature.Model;
    var serviceRoot = new Uri( new Uri( feature.BaseAddress ), feature.RoutePrefix );
    var requestProvider = feature.Services;
    var parser = new ODataUriParser( model, serviceRoot, link, requestProvider );

    parser.Resolver ??= new UnqualifiedODataUriResolver() { EnableCaseInsensitive = true };
    parser.UrlKeyDelimiter = ODataUrlKeyDelimiter.Slash;

    var path = parser.ParsePath();
    var segment = path.OfType<KeySegment>().Single();
    var otherKey = segment.Keys.Single().Value; // note: could have multiple keys

    // TODO: use 'otherKey'

    return NoContent();
}

It took some spelunking, but this is based on what OData does to parse paths (as seen here). It's unclear why, but both the corresponding interface and implementation are marked internal. With a little extra work, you could turn this into an extension method.

Diu answered 1/6, 2022 at 17:54 Comment(4)
Thanks a lot Chris, really appreciate your work! Is there a timeline when we'll have stable version of Asp.Versioining.OData?Isotone
I'm hoping the next 3-4 weeks max. Burn-in has been solid. Coding signing needs to be solved and is slow-going. A bug was also discovered in ASP.NET Core that affects Minimal APIs w/OpenAPI, but otherwise, things are on-track for a stable release.Diu
Hi! just wanted to check if the timeline for stable version release was changed?Isotone
Still waiting on this issue to be completed for code signing, but - otherwise - yes. I've been working informal back channels over email too, but it's moving slow. I don't understand the hold-up, but I'm doing what I can to move it forward. Eager to start work for .NET 7 soon. :)Diu

© 2022 - 2024 — McMap. All rights reserved.