具有WebAPi OData属性路由的$ Metadata不起作用 [英] $Metadata with WebAPi OData Attribute Routing Not Working

查看:254
本文介绍了具有WebAPi OData属性路由的$ Metadata不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为OData端点使用OData属性路由.这是我所拥有的示例:

I'm using OData Attribute Routing for an OData endpoint. Here is an example of what I have:

[ODataRoutePrefix("Profile")]
public class ProfileODataController : ODataController
{
    [ODataRoute]
    [EnableQuery]
    public IHttpActionResult Get()
    {
        var repo = new Repositories.ProfileRepository();

        return Ok(repo.GetProfiles());
    }

    [ODataRoute("({key})")]
    [EnableQuery]
    public IHttpActionResult Get([FromODataUri] string key)
    {
        var repo = new Repositories.ProfileRepository();

        var result = repo.GetProfiles().SingleOrDefault(x => x.Id== key);
        if (result == null) return NotFound();

        return Ok(result);
    }
}

这是我的设置:

config.MapODataServiceRoute("odata", "odata", ModelGenerator.GetEdmModel());

这是我的EdmModel一代:

Here is my EdmModel Generation:

public static IEdmModel GenerateEdmModel()
{
    var builder = new ODataConventionModelBuilder();

    builder.EntitySet<Profile>("Profile").EntityType.HasKey(x => x.Id);

    return builder.GetEdmModel();
}

URL /odata/Profile/odata/Profile('someid')都可以,但是当我尝试访问$ metadata端点(/odata/$metadata#Profile)时,出现以下错误:

The urls /odata/Profile and /odata/Profile('someid') both work, but when I try to access the $metadata endpoint (/odata/$metadata#Profile), I get the following error:

{"Message":未找到与请求URI'http:// **** /odata/$ metadata'相匹配的HTTP资源.","MessageDetail":否找到与名为元数据"的控制器匹配的类型.}

{"Message":"No HTTP resource was found that matches the request URI 'http://****/odata/$metadata'.","MessageDetail":"No type was found that matches the controller named 'Metadata'."}

我需要创建控制器/操作来提供元数据吗?如果是这样,该行动如何实施?

Do I need to create a controller/action for serving the metadata? If so, how is that action implemented?

推荐答案

原来,这与我替换IAssembliesResolver有关.

Turns out it had to do with my replacement of the IAssembliesResolver.

我已经实现了自定义版本,以仅提供实现了控制器的 component 程序集.但是,正如错误所言,它找不到名为MetadataController的控制器.事实证明,OData实现了一个:System.Web.OData.MetadataController,它提供了$metadata关键字.

I had implemented a custom version to provide only the component assemblies that I had implemented controllers in. However, as the error said, it couldn't find a controller named MetadataController. Turns out, OData implements one: System.Web.OData.MetadataController, which provides for the $metadata keyword.

由于我已经实现了自己的IAssembliesResolver,因此未包含System.Web.OData程序集,并且$metadata失败.一旦发现了这一点,并更新了程序集解析器以明确包含OData程序集,它现在就可以正常工作了.

Since I had implemented my own IAssembliesResolver, the System.Web.OData assembly wasn't being included, and $metadata failed. Once I discovered this, and updated my assembly resolver to explicitly include the OData assembly, it now works as it should.

这篇关于具有WebAPi OData属性路由的$ Metadata不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆