Generic Controller not working
@xuzhg is already working on this.
Since Nov 15, 2022.
Assessment
This issue has not been assessed yet.
Description
Currently using 8.0.11
Return response not being formatted to odata response IE not including @odata.Context
Actual Result

Expected Result

Reproduce steps
In Main during the builder process
ODataConventionModelBuilder odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<TestObj>("TestObj");
builder.Services.AddControllersWithViews()
.AddOData(options =>
{
options.EnableQueryFeatures();
options.AddRouteComponents("api/odata", odataBuilder.GetEdmModel());
})
.ConfigureApplicationPartManager(pm =>
{
pm.FeatureProviders.Add(new TestProvider());
})
.AddODataNewtonsoftJson()
.AddNewtonsoftJson();
###Model / Controller setup
public class TestObj
{
public int Id { get; set; }
public string? Name { get; set; }
}
[GenericControllerRouteConvention]
public class OdataController<T> : ODataController
{
[HttpGet]
[EnableQuery]
public IQueryable<T> Get()
{
return (new List<T>()).AsQueryable();
}
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class GenericControllerRouteConvention : Attribute, IControllerModelConvention
{
public void Apply(ControllerModel controller)
{
if (controller.ControllerType.IsGenericType)
{
Type? genericType = controller.ControllerType.GenericTypeArguments[0];
var name = genericType.Name;
controller.ControllerName = name + "Controller";
controller.Selectors.Add(new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel(new RouteAttribute("api/odata/" + name)),
});
}
}
}
public class TestProvider : IApplicationFeatureProvider<ControllerFeature>
{
public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
{
TypeInfo? controllerType = typeof(OdataController<>)
.MakeGenericType(typeof(TestObj)).GetTypeInfo();
feature.Controllers.Add(controllerType);
}
}
EDM (CSDL) Model

Request/Response
https://localhost/api/odata/TestObj
Expected behavior

Additional context
I plan on using this to auto generate hundreds of odata endpoints and although this code is simple and only for one model it does reproduce the bug.
Everything works if I defined the controller explicitly
public class TestObjController : ODataController
but that is not a viable solution when you have hundreds of models.
- Dominant language
- C#
- Stars
- 505
- Forks
- 186
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from OData/AspNetCoreOData
-
.NET 10 support Open
Difficulty 5/5 Over a week Newbie friendliness 25/100
OData/AspNetCoreOData#1610 · 2 comments · 5 reactions ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 68/100
OData/AspNetCoreOData#1609 · 2 comments ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 72/100
OData/AspNetCoreOData#1584 ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 72/100
OData/AspNetCoreOData#1572 ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 58/100
OData/AspNetCoreOData#1569 · 2 comments ·
All issues in OData/AspNetCoreOData
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100