Skip to content

/$metadata returns 404 when there's no action methods in ODataController #1137

@pycxu

Description

@pycxu

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

/$metadata returns 404 when there's no action methods in ODataController.

Startup:

    services.AddControllers().AddOData(options => options.Expand().Select().Filter().OrderBy().Count().SetMaxTop(10));
    services.AddApiVersioning(opt =>
    {
        opt.DefaultApiVersion = new Asp.Versioning.ApiVersion(1, 0);
        opt.AssumeDefaultVersionWhenUnspecified = true;
        opt.ReportApiVersions = true;
    }).AddOData(options => options.AddRouteComponents("odata/v{version:apiVersion}"));

Controller:

    public class TestAController : ODataController
    {
        [HttpPost("api/Test")]
        public IActionResult Test()
        {
            return (Created(new[] { new TestA { Id = 1 }, new TestA { Id = 2 } }));
        }
    }

Endpoints:

test: Information: Route: odata/v{version:apiVersion}/$metadata  |  Name: Asp.Versioning.Controllers.VersionedMetadataController.GetOptions (Asp.Versioning.OData)
test: Information: Route: odata/v{version:apiVersion}/$metadata  |  Name: Asp.Versioning.Controllers.VersionedMetadataController.GetMetadata (Asp.Versioning.OData)
test: Information: Route: odata/v{version:apiVersion}  |  Name: Asp.Versioning.Controllers.VersionedMetadataController.GetServiceDocument (Asp.Versioning.OData)

GET http://localhost:57464/odata/v1.0/$metadata returns the metadata;

but if I comment out the actions in Controller, it returns 404 instead.

Controller (with no actions):

    public class TestAController : ODataController
    {
        //[HttpPost("api/Test")]
        //public IActionResult Test()
        //{
        //    return (Created(new[] { new TestA { Id = 1 }, new TestA { Id = 2 } }));
        //}
    }

Expected Behavior

GET http://localhost:57464/odata/v1.0/$metadata should return metadata even there's no ODataController or actions?

Steps To Reproduce

I have attached a repro project for your reference.
Repro.zip

Exceptions (if any)

No response

.NET Version

8

Anything else?

I'm not sure if this is intended or a bug.
Please advise if I'm not doing things correctly.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions