user6269864
user6269864

Reputation:

OData queries give 404 error unless I add a trailing slash

I created a dummy project to test Odata in VS2015 and I was having an exactly the same issue as described in this question, and my code is largely equivalent to what is described there. Web API 2: OData 4: Actions returning 404

Any query to a bound function gives a 404 error until you add a trailing slash. For example:

http://localhost:46092/odata/v1/Trips/Default.GetTripNameById - 404 http://localhost:46092/odata/v1/Trips/Default.GetTripNameById/ - works as expected

http://localhost:46092/odata/v1/Trips/Default.GetTripNameById(tripID=1)?$select=Name - 404 http://localhost:46092/odata/v1/Trips/Default.GetTripNameById(tripID=1)/?$select=Name - works as expected

This is not supposed to happen because Microsoft documentation never mentions that a trailing slash is required, their examples are supposed to work without them. Also, this breaks the Swagger UI which doesn't add a trailing slash and gets 404 when trying to make any query.

What could be the reason for this behaviour? How do I make it work without a slash, which seems to be the normal expected behaviour?

Here are my code snippets:

TripsController.cs:

    ...
    [HttpGet]
    public IHttpActionResult GetTripNameById(int tripID)
    {
        return Ok(DemoDataSources.Instance.Trips.AsQueryable().Where(t => t.ID == tripID.ToString()));
    }

WebApiConfig.cs:

public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.MapODataServiceRoute("odata", "odata/v1", GetEdmModel());

DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
        config.EnsureInitialized();
    }

    private static IEdmModel GetEdmModel()
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Person>("People");
        builder.EntitySet<Trip>("Trips");

        builder.EntityType<Trip>().Collection.Function("GetTripNameById").Returns<string>().Parameter<int>("tripID");

        var edmModel = builder.GetEdmModel();
        return edmModel;
    }

Upvotes: 0

Views: 547

Answers (1)

user6269864
user6269864

Reputation:

It turns out that the Web.debug.config is actually ignored by Visual Studio.

After adding this code to web.config, everything works:

<system.webServer>
<handlers>
  <!-- the following line is required for correct handling of dots in URLs-->
  <add name="ApiURIs-ISAPI-Integrated-4.0"
     path="/odata/*"
     verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
     type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />
  <!-- end line for handling of dots-->
</handlers>
</system.webServer>

Upvotes: 2

Related Questions