1
Answer

WebApi OData Delete routing convention

I have a MVVM project. For the model I use a EF6.0.0 code first and WebApi.

In general everything works fine but for one thing.

When I do a delete the following URL is composed

http://localhost:50346/Recruiters/Addresses(guid'5d778c9d-56b2-449b-b655-22489e01636d')/CIP.Models.Domain.Addresses

and this results in a 404 error.

So I've created an routing convention like this:

 
using Microsoft.Data.Edm; 
using
System;
using
System.Collections.Generic;
using
System.Diagnostics.Contracts;
using
System.Linq;
using
System.Net.Http;
using
System.Web;
using
System.Web.Http.Controllers;
using
System.Web.Http.OData.Routing;
using
System.Web.Http.OData.Routing.Conventions;

namespace CIP
{
   public class AddressesRoutingConvention : EntitySetRoutingConvention
   {
      public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap)
      {
         if (odataPath.PathTemplate == "~/entityset/key/cast")
         {
            HttpMethod httpMethod = controllerContext.Request.Method;
            string httpMethodName;
            switch (httpMethod.ToString().ToUpperInvariant())
            {
                 case "DELETE":
                   httpMethodName = "Delete";
                   break;
                 default:
                     return null;
             }

            Contract.Assert(httpMethodName != null);
            IEdmEntityType entityType = odataPath.EdmType as IEdmEntityType;

            string actionName = httpMethodName + entityType.Name;

            if (actionName != null)
            {
                KeyValuePathSegment keyValueSegment = odataPath.Segments[1] as KeyValuePathSegment;
                controllerContext.RouteData.Values[ODataRouteConstants.Key] = keyValueSegment.Value;

               return actionName;
            }
         } // Not a match return null;
      }
    }
 }

and added this route

   var conventions = ODataRoutingConventions.CreateDefault();
   conventions.Insert(0, new AddressesRoutingConvention());
   config
.Routes.MapODataRoute("Addresses", "Addresses", addressesBuilder.GetEdmModel(), new DefaultODataPathHandler(), conventions);`

And in the controller

   public async Task<IHttpActionResult> DeleteAddresses([FromODataUri] Guid key)
    {
       Addresses addresses = await db.Addresses.FindAsync(key);

      if (addresses == null)
       {
          return NotFound();
       }
      db
.Addresses.Remove(addresses);
      await db.SaveChangesAsync();
       return StatusCode(HttpStatusCode.NoContent);
      }

But still I get the 404 error.

I tried testing it from SOAPUI with the same result.

Am I missing something?

Kind regards

Jeroen

Answers (1)