我对OData端点使用OData属性路由。这里是什么,我有一个例子:
[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代:
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')
都工作,但是当我尝试访问$元数据端点(/odata/$metadata#Profile
)时,出现以下错误:
{"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'."}
是否需要创建一个控制器/操作用于提供元数据?如果是这样,该行动如何实施?
你可以发表你的'ModelGeneratlr.GetEdmModel()'吗? – Blaise 2014-09-12 14:32:11
@Blaise发表。 – 2014-09-12 17:23:09
你的代码没有什么特别的。不需要额外的操作来提供元数据。您应该可以使用'.../odata /?$ metadata'和'.../odata/$ metadata'查看元数据。 – Blaise 2014-09-12 19:47:42