Go Support for Composite Key in ASP. NET Web API OData

Source: Internet
Author: User
This article goes from:

He default Entitysetcontroller doesn ' t support composite keys. So if you had composite key models, you need some additional work. Here's an example on how-to-do.

The model is simple:

public class Person{    [Key]    public string FirstName { get; set; }    [Key]    public string LastName { get; set; }    public int Age { get; set; }}

The OData URL for this model would look like:

GET http://localhost:33051/People(FirstName='Kate',LastName='Jones') HTTP/1.1

And we want to the strong typed parameters in the Web API actions to the this URL.

    public Person Get([FromODataUri] string firstName, [FromODataUri] string lastName)

Note that the Fromodatauri Model binder attribute are used to parse from the OData URI representation to the CLR type. In OData, string value was "' xxx '" and we want it to be "xxx".

In order to do the route to work, you can add a custom routing convention to parse the key path. Here is a sample implementation:

public class compositekeyroutingconvention:entityroutingconvention{public override string SelectAction (system.web.h Ttp. OData.Routing.ODataPath Odatapath, System.Web.Http.Controllers.HttpControllerContext ControllerContext, ILookup <string, system.web.http.controllers.httpactiondescriptor> ActionMap) {var action = base.        SelectAction (Odatapath, ControllerContext, ActionMap);            if (action! = null) {var routevalues = controllerContext.RouteData.Values; if (Routevalues.containskey (Odatarouteconstants.key)) {var Keyraw = Routevalues[odatarouteconst Ants.                Key] As String;                ienumerable<string> compoundkeypairs = Keyraw.split (', ');                if (Compoundkeypairs = = NULL | | Compoundkeypairs.count () = = 0) {return action; } foreach (Var compoundkeypair in compoundkeypairs) {string[] pa IR = CompouNdkeypair.split (' = '); if (pair = = NULL | | pair.                    Length! = 2) {continue; } var keyName = pair[0].                    Trim (); var keyValue = pair[1].                    Trim ();                Routevalues.add (KeyName, KeyValue);    }}} return action; }}

The Convention is inherited from Entityroutingconvention, which are the default convetion to handle entity key. by calling base. SelectAction, it'll add the full key path into routevalues. The new convention would check if it contains "," and seperate it into multiple keys and set each of the them into routevalues. So when Web API select actions, it'll use those values to determine which action to choose. If There is no "," found, it behaves same as base convetion.

To register the convetion, you need to set it when mapping OData route:

public static class WebApiConfig{    public static void Register(HttpConfiguration config)    {        config.EnableQuerySupport();        var mb = new ODataConventionModelBuilder(config);        mb.EntitySet<Person>("People");        var conventions = ODataRoutingConventions.CreateDefault();        conventions.Insert(0, new CompositeKeyRoutingConvention());        config.Routes.MapODataRoute(            routeName: "OData",             routePrefix: null,             model: mb.GetEdmModel(),             pathHandler: new DefaultODataPathHandler(),             routingConventions: conventions);    }}

Register the route at the Postion 0 was to make it was executed before other default routing convetions. So the default entityroutingconvetion won ' t is executed before it. After this, you should is able to get the routing work.

Then, how to build the URL for composite keys?
You don't need to do the for OData links include edit link and self link when using Odataconventionmodelbuilder. It'll automatically identify composite keys and build the URI for you.

However, need to build the link for location header. Here are a sample code from PeopleController.cs to handle POST request:

public HttpResponseMessage PostPerson(Person person){    if (ModelState.IsValid)    {        _repo.UpdateOrAdd(person);        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, person);        string key = string.Format(            "{0}={1},{2}={3}",            "FirstName", ODataUriUtils.ConvertToUriLiteral(person.FirstName, Microsoft.Data.OData.ODataVersion.V3),            "LastName", ODataUriUtils.ConvertToUriLiteral(person.LastName, Microsoft.Data.OData.ODataVersion.V3));        response.Headers.Location = new Uri(Url.ODataLink(            new EntitySetPathSegment("People"),            new KeyValuePathSegment(key)));        return response;    }    else    {        return Request.CreateResponse(HttpStatusCode.BadRequest);    }}

Hope it helps.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.