OData 4 web api 2"未找到路由约定"

时间:2016-01-08 13:21:57

标签: asp.net-web-api odata

我试图将对象发布到odata动作,这里是我的代码

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);
        }
    }
}

这是我的模特

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

这是我的OData路线

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();
}

我的电话是这个

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

这是我的回复

{
    "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": ""
        }-
    }-
}

我曾尝试将名称空间添加到odata路线,但它不起作用 有任何想法吗? 感谢

1 个答案:

答案 0 :(得分:1)

该文档可能有所帮助:http://odata.github.io/WebApi/#04-07-action-parameter-support 并且在没有命名空间的情况下调用,您需要打开UnqualifiedNameCall选项,如:

config.EnableUnqualifiedNameCall(true);