4

I have an OData v4 action method which is not working; note however that it was working fine in OData v3 (I am obviously in the process of trying to update my project)

OData Action Method:

[HttpPost]
public Translation Translate(ODataActionParameters parameters)
{
    // Implementation
}

Configuration:

ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Page>("Pages");
//etc (Other Entity Sets)

var pageEntityType = builder.EntityType<Page>();
var translateAction = pageEntityType.Collection.Action("Translate");
translateAction.Parameter<Guid>("pageId");
translateAction.Parameter<string>("cultureCode");
translateAction.Returns<Translation>();

//etc (Other Actions)

var route = config.MapODataServiceRoute("OData_CMS", "odata/cms", builder.GetEdmModel());

Client AJAX Call:

var data = {
    pageId: $("#CultureSelector_PageId").val(),
    cultureCode: $("#CultureSelector_CultureCode").val()
};

$.ajax({
    url: "/odata/cms/Pages/Translate",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(data),
    dataType: "json",
    async: false
})
.done(function (json) {
    //etc

I tried to see if anything has changed regarding setup for OData actions in version 4, but it seems the same (refer to: Actions and Functions in OData v4 Using ASP.NET Web API 2.2)

EDIT

I found out that OData v4 uses a Default namespace and implemented that, as follows:

Firstly, just by changing my AJAX call to:

url: "/odata/cms/Pages/Default.Translate",

That didn't work, so I also added:

[ODataRoute("Default.Translate")] and

[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]

to my action, as per the instructions at this link: http://damienbod.wordpress.com/2014/06/16/web-api-and-odata-v4-crud-and-actions-part-3/..

Also not working.. I have followed the steps to the letter... either I'm being blind and missing something here or there's a serious problem with the latest version of OData for Web API.

Matt
  • 6,787
  • 11
  • 65
  • 112
  • Check this [post](http://stackoverflow.com/questions/32063196/odata-v4-function-always-returns-404/39438274#39438274) for a solution. – rdhainaut Sep 12 '16 at 15:25
  • If you want a complete solution, Check this [answer](http://stackoverflow.com/questions/32063196/odata-v4-function-always-returns-404/39438274#39438274) ;) Et Voilà – rdhainaut Sep 12 '16 at 15:27

4 Answers4

8

This may be caused by the routing convention of IIS, which would have its own routing rule when Uri contains dot. In odata v4, however, all function/action calls are required to be namespace qualified. Then there would be a dot appearing in such Uri, which would be mis-handled by IIS.

To get rid of this, you could try either of followings:

  1. Turn on runAllManagedModulesForAllRequests, add the following in Web.config

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

But there can be some potential issue for this option, please refer to this post for detail.

  1. Turn on project specific settings, add the following in Web.config:

    <system.webServer>
        <handlers>
            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <add name="ExtensionlessUrlHandler-Integrated-4.0" path="odata/cms*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
    </system.webServer>

Karata
  • 1,079
  • 8
  • 16
  • Thanks. For the moment, I had to roll back to v3 since I have much to get done. But I will try this solution asap.. in the next few days probably and get back to you. Appreciate the suggestion, thanks again. – Matt Sep 30 '14 at 00:49
  • I found that the handler for ExtensionlessHandler-Integrated-4.0 already existed in the web.config, but it's original path="*.", was not working. If you just add path="*.*" it can handle any path for odata routing. That way you don't limit it to "odata/cms". – Todd Meinershagen Jun 15 '17 at 04:24
6

I had the same problem and I solved adding a trailing slash to the url. In your case it would be /odata/cms/Pages/Translate/

1

Well, it was almost a year after this question that I actually tried moving to OData v4 again and had the same problem. I forgot about my original question here and asked a new one and then found the answer. See OData v4 Function always returns 404 for more details. I'm glad to say all is working well now.

Community
  • 1
  • 1
Matt
  • 6,787
  • 11
  • 65
  • 112
0

Do you use Entity Framework Database First Approuche? Take a look at navigation properties, at serialization time they may be holding. In my case, I remove all the navigation properties just for testing, and it works.

MattAllegro
  • 6,455
  • 5
  • 45
  • 52