Search code examples
asp.net-web-apiodata

OData 4 web api 2 "No routing convention was found"


I'm trying to post object to odata action, here my code

public class DraftController : ODataController
{
    public HttpResponseMessage Attachment([FromODataUri] string key, [FromBody] DraftApi d)
    {
        try
        {
            return Request.CreateResponse(HttpStatusCode.Created, "(POST ATTACHMENT) key: " + key + " - id: " + d.id + ", desc: " + d.description);
        }
        catch (Exception e)
        {
            return Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message);
        }
    }
}

this is my model

public class DraftApi
{
    [Key]
    public string id { get; set; }
    public string description { get; set; }
}

this is my OData route

config.MapODataServiceRoute(
    routeName: "ODataDraft",
    routePrefix: "odata/{namespace}",
    model: BuildModel<DraftApi>("draft")
);
private static IEdmModel BuildModel<T>(string EntityName) where T : class
{
    ODataConventionModelBuilder ODataBuilder = new ODataConventionModelBuilder();
    ODataBuilder.EntitySet<T>(EntityName).EntityType.Name = EntityName;

    ActionConfiguration attachment = ODataBuilder.EntityType<T>().Action("Attachment");

    ODataBuilder.EnableLowerCamelCase();
    return ODataBuilder.GetEdmModel();
}

my call is this

Url
http://127.0.0.1/mpssapi/odata/v1/draft('hhh')/Attachment
Headers
Content-Type: application/json
Payload
{id:"a", description: "abc"}

This is my response

{
    "error": {
        "code": ""
        "message": "No HTTP resource was found that matches the request URI 'http://127.0.0.1/mpssapi/odata/v1/draft('hhh')/Attachment'."
        "innererror": {
        "message": "No routing convention was found to select an action for the OData path with template '~/entityset/key/unresolved'."
        "type": ""
        "stacktrace": ""
        }-
    }-
}

I have tried to add namespace to odata route but it doesn't work any ideas? thanks


Solution

  • The doc may help: http://odata.github.io/WebApi/#04-07-action-parameter-support and call without namespace, you need to turn on UnqualifiedNameCall option like:

    config.EnableUnqualifiedNameCall(true);