Kuttan Sujith
Kuttan Sujith

Reputation: 7979

OData gives result only after making a web API request

I have created an Odata in web Api 2

I am adding routes like

config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: "odata",
            model: GetModel()
        );

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        ); 

I am getting a strange result.

If I hit the OData service first (after a building/publishing) it is showing the below image. enter image description here

But if I hit an api request first and making the OData request next,

then I am getting the expected JSON.

In short an OData request works only after making an API request.

Can anyone say what would be reason for this behavior?

Upvotes: 0

Views: 40

Answers (1)

lencharest
lencharest

Reputation: 2925

Order matters. Try moving the OData config after the Web API config:

config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    ); 

config.MapODataServiceRoute(
        routeName: "ODataRoute",
        routePrefix: "odata",
        model: GetModel()
    );

If you don't have any regular Web API controllers, eliminate the call to MapHttpRoute entirely.

Upvotes: 1

Related Questions