Search code examples
c#asp.netodataasp.net-web-api

Web Api Odata action methods using Get verb return 404


I'm trying to expose a document stream as an Action Method on a specific type. It would look something like this:

/odata/MyType(123)/stream <-- this returns a binary stream of data.

When accessing the above endpoint using GET I only get a 404, even though the endpoint is exposed as [HttpGet] on the Controller.

Sematically it would make sense to access this resource using the Get verb, as this is simply an operation for retrieval of data, and not side-effecting.

So far I've only gotten this to work by exposing the action method under the POST-verb.

The action method controller implementation currently look like this:

[HttpPost] // <-- I want this to be [HttpGet]
public HttpResponseMessage Test([FromODataUri] int key, ODataActionParameters parameters)
{
    var fileStream = File.OpenRead(@"c:\somefile");

    return new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StreamContent(fileStream)
        };
}

The code for registering the action method on 'MyType'

var entityTypeConfiguration = mapper.Builder.Entity<MyType>();
var actionConfiguration = entityTypeConfiguration.Action("stream");
actionConfiguration.Returns<HttpResponseMessage>();

The implementation is inspired by this article: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions

Anyone know how I can possible expose this action method as a Get?


Solution

  • OData has better semantics for exposing streams for an entity. You can either use named streams or Media Link Entry (MLE). Read about it here and here.

    Named streams seem to suit your scenario better. I have a sample here that shows how to use extensibility points in web API OData to do named streams.