Search code examples
c#apiroutesodata

OData: Custom Action never works


I'am getting crazy here. I tried a lot of different approaches that I found through out the web. However cannot seem to get this work. Here is the last thing I tried.

modelBuilder.EntitySet<User>("Users").EntityType.Action("IsEmailAvailable")
.Returns<bool>()
.Parameter<string>("email");

I configure my entity like this.

[HttpPost]
public bool IsEmailAvailable(ODataActionParameters parameters)
{
    return true;
}

And this is the action in my controller.

When I do something like

http://localhost:11111/odata/Users

I gets the entites as expected. However if I do

http://localhost:11111/odata/Users/IsEmailAvailable

I got the error:

Message: "The OData path is invalid.", ExceptionMessage: "Invalid action detected. 'IsEmailAvaliable' is not an action that can bind to 'Collection([App.Models.Models.User Nullable=False])'.", ExceptionType: "Microsoft.Data.OData.ODataException", StackTrace: " at System.Web.Http.OData.Routing.DefaultODataPathHandler.ParseAtEntityCollection(IEdmModel model, ODataPathSegment previous, IEdmType previousEdmType, String segment) at System.Web.Http.OData.Routing.DefaultODataPathHandler.ParseAtCollection(IEdmModel model, ODataPathSegment previous, IEdmType previousEdmType, String segment) at System.Web.Http.OData.Routing.DefaultODataPathHandler.ParseNextSegment(IEdmModel model, ODataPathSegment previous, IEdmType previousEdmType, String segment) at System.Web.Http.OData.Routing.DefaultODataPathHandler.Parse(IEdmModel model, String odataPath) at System.Web.Http.OData.Routing.ODataPathRouteConstraint.Match(HttpRequestMessage request, IHttpRoute route, String parameterName, IDictionary`2 values, HttpRouteDirection routeDirection)"


Solution

  • Allright, after calming down, because I got crazy why this was not working, I started to think and find the cause of the problem. Somehow I installed both V1-3 and V4 versions of Odata. So when I was using builders it was getting confused. So I unistalled the V1-3 and fixed the compiler errors and It is working fine. Thanks to Sam Xu for his efforts.