Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Api 2.2 OData V4 Function Routing

I have a Web Api 2.2 project working with OData v4. The normal EntitySet configuration is working as desired with all http verbs. Where I am having a problem is trying to expose a custom function. I started off trying to do something different than the standard examples, but I have backed all the way up to just trying to getting a basic example function working.

Here is my startup config (straight from the MS examples):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;

namespace Test.Service
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            // other entitysets that don't have functions

            builder.EntitySet<Product>("Products");
            builder.Namespace = "ProductService";
            builder.EntityType<Product>().Collection
                .Function("MostExpensive")
                .Returns<double>();

            config.MapODataServiceRoute(
                "odataroute"
                , "odata"
                , builder.GetEdmModel()                        
                );
        }
    }
}

And here is my controller:

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.OData;

namespace Test.Service.Controllers
{
    public class ProductsController : ODataController
    {
        private EntityContext db = new EntityContext();

        [EnableQuery]
        public IQueryable<Product> GetProducts()
        {
            return db.Products;
        }

        [HttpGet]
        public IHttpActionResult MostExpensive()
        {
            double test = 10.3;
            return Ok(test);
        }
    }
}

The regular GET, works fine:

http://something/odata/Products

However, the following always gives me a 404:

http://something/odata/Products/ProductService.MostExpensive()

I have tried any number of different things with the namespace, etc... So, it doesn't work like all of the examples, but I'm at a loss at how to dig in deeper to figure out what is going wrong. The metadata exposed by http://something/odata doesn't provide any clues. Is there any other way to discover where (and how) this function should be exposed?

EDIT: Here is the link to the Microsoft Example I am following: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-and-functions

like image 574
snow_FFFFFF Avatar asked Aug 14 '14 15:08

snow_FFFFFF


2 Answers

Please change the element as below, which is the recommended way if there is dot in the request URL:

 <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
 </system.webServer>

and if

http://something/odata/Products/ProductService.MostExpensive()

is requested, I can get the data:

{
@odata.context: "http://localhost:14853/odata/$metadata#Edm.Double",
value: 3
}
like image 191
Tan Jinfu Avatar answered Sep 28 '22 18:09

Tan Jinfu


I know this question is not recent, but I found another answer that works for me. If you're willing to remove the namespace from the URL, you can use

config.EnableUnqualifiedNameCall(true);

Your URL would then look like this:

http://something/odata/Products/MostExpensive

See http://odata.github.io/WebApi/#06-01-custom-url-parsing. That's available in the Microsoft.AspNet.OData NuGet package.

like image 42
Don 01001100 Avatar answered Sep 28 '22 17:09

Don 01001100