Click here to Skip to main content
15,885,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
From an MVVM application I get the following URL for delete:
http://localhost/Recruiters/Addresses(guid '7777-777')/CIP.Domain.Addresses

I did not fill complete guid in the example URL, but it is a valid one.

This URL is not a normal OData route, so I created an routing convention
C#
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 that to my OData controller.
C#
var conventions = ODataRoutingConventions.CreateDefault();
conventions.Insert(0, new AddressRoutingConvention());

config.Routes.MapODataRoute("Addresses", "Addresses", addressesBuilde.GetEdmModel, new DefaultODataPathHandler(), conventions);



And in my controller:
C#
public asyn Task<IHttpActionresult> Delete([FromODataUri Guid key)
{

}


But I still get a not found error when testing.

Any ideas?

Kind regards

Jeroen
Posted

1 solution

There were two problems with this as it turns out:

First:
My web.config was not properly set up to handle a dot in the URL.
So I changed it to this:
XML
<system.webserver>
        <handlers>
          <clear />
          <add name="ExtensionlessUrlHandler-Integrated-4.0" path="/*">
              verb="*" type="System.Web.Handlers.TransferRequestHandler" 
          preCondition="integratedMode,runtimeVersionv4.0" />
        </add></handlers>
    </system.webserver>  


Also in my URL i use Recruiters, but I added the routeconvention to the Addresses route.

So I also added it to the Recruiter route like this:
C#
config.Routes.MapODataRoute("Recruiters", "Recruiters", recruitersBuilder.GetEdmModel(), new DefaultODataPathHandler(), conventions);


And it works.

Jeroen
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900