asp.net core 2.1 Odata How to omit “@odata.type” in response

[亡魂溺海] 提交于 2021-02-11 13:24:50

问题


I need help to remove from my response the odata type "@odata.type". I read in Odata documentation The odata.type annotation MUST appear if the type cannot be heuristically determined, as described below, and one of the following is true:

· The type is derived from the type specified for the (collection of) entities or (collection of) complex type instances, or

· The type is for a property whose type is not declared in $metadata. but I cannot figure out how to do it.

{
    "@odata.context": "https://localhost:44396/odata/v1/rh/employe-training/$metadata#Collection(RH.Common.Modeles.EmployeTraining)",
    "@odata.count": 2593,
    "value": [
        {
            "@odata.type": "#RH.Common.Modeles.EmployeTraining",
            "training_code": "ACC9701",
            "description": "Prime",
            "statut": null            
        },
        {
            "@odata.type": "#RH.Common.Modeles.EmployeTraining",
            "training_code": "BC-02356",
            "description": "Virtuo",
            "statut": null            
        }
    ],
    "@odata.nextLink": "https://localhost:44396/odata/v1/rh/employe-training?$count=true&pagesize=2&$skip=2"
}

EdmModelBuilder.cs

public static class EdmModelBuilder
{
    public static IEdmModel GetEdmModelEmployeTraining()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<EmployeTraining>("employe-training");            
        return builder.GetEdmModel();
    }

    public static IEdmModel GetEdmModelPersons()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<Person>("Person");            
        return builder.GetEdmModel();
    }

}

Startups.cs

app.UseMvc(routeBuilder=>
        {                
            routeBuilder.Expand().Select().Count().OrderBy().Filter().MaxTop(null);
            routeBuilder.MapODataServiceRoute("EmployeTraining", "odata/v1/rh/employe-training", EdmModelBuilder.GetEdmModelEmployeTraining());
            routeBuilder.MapODataServiceRoute("Person", "odata/v1/rh/person", EdmModelBuilder.GetEdmModelPersons());

        });

Controllers.cs

public class EmployeTrainingController : ODataController
{
    internal IEmployeTrainingService ServiceEmploye { get; set; }

    public EmployesController(IEmployeTrainingService serviceEmploye)
    {

        ServiceEmployeTraining = serviceEmployeTraining;
    }   
    [HttpGet]
    [ODataRoute("", RouteName = "EmployeTraining")]
    [MyCustomQueryable()]
    public IQueryable<EmployeTraining> Get()
    {

        return ServiceEmploye.GetListeEmployeTraining();
    }
}

来源:https://stackoverflow.com/questions/62157807/asp-net-core-2-1-odata-how-to-omit-odata-type-in-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!