$ Metadata with OAP attribute attribute for WebAPi does not work
I am using OData Attribute Routing for OData endpoint. Here's 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);
}
}
Here's my setup:
config.MapODataServiceRoute("odata", "odata", ModelGenerator.GetEdmModel());
Here is my generation of EdmModel:
public static IEdmModel GenerateEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Profile>("Profile").EntityType.HasKey(x => x.Id);
return builder.GetEdmModel();
}
The urls /odata/Profile
and /odata/Profile('someid')
work, but when I try to access the $ metadata ( /odata/$metadata#Profile
) endpoint , I get the following error:
{"Message": "No HTTP resource found that matches the request URI" http: // **** / odata / $ metadata ".," MessageDetail ":" No type was found that matches a controller named "Metadata ". "}
Do I need to create a controller / action to serve the metadata? If so, how is this action implemented?
source to share
It turns out it has something to do with my replacement IAssembliesResolver
.
I implemented my own version to only expose the component assemblies into which I injected the controllers. However, as the error said, it could not find the controller named MetadataController
. It turns out OData implements one: System.Web.OData.MetadataController
which provides the keyword $metadata
.
Since I did my own IAssembliesResolver
, the build System.Web.OData
didn't turn on but rather $metadata
crashed. Once I discovered this and updated my collector to explicitly include the OData assembly, it now works as it should.
source to share