13
votes

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

3
Just curious,have you tried: something/odata/Products/MostExpensive?Andrea Scarcella
Yes, I've tried any number of different combinations: like odata/Products/MostExpensive, odata/Products/MostExpensive(), odata/Products/Default.MostExpensive() (the last one when I didn't explicitly set the namespace)snow_FFFFFF
Thank you, I also noticed there is no response type attribute [ResponseType(typeof(decimal))].Andrea Scarcella
Thanks for the suggestion, but the ResponseType attribute doesn't seem to be available in this version of WebApi (or I am missing a using...). And, in all of the examples I have seen, this isn't necessary (I added a link to the MS example above). What is weird is that I can add an unbound function without issue, I just can't bind a function to an entity set.snow_FFFFFF

3 Answers

17
votes

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
}
7
votes

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.

2
votes

Then you may try adding the part not replacing them. Mine looks like below and it can work.

<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  <clear/>
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="/*"
      verb="*" type="System.Web.Handlers.TransferRequestHandler"
      preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>