asp.net core 2.1 odata在路由中使用不同的实体名称

问题描述 投票:0回答:2

我在代码EmployeTraining中拥有实体的全名,该名称在OData中用作实体,并且与控制器具有相同的名称。

Startup.cs

 app.UseMvc(routeBuilder=>
        {                
            routeBuilder.Expand().Select().Count().OrderBy().Filter().MaxTop(null);
            routeBuilder.MapODataServiceRoute("EmployeTraining", "odata/v1", EdmModelBuilder.GetEdmModelEmploye());

        });


EdmModelBuilder.cs

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

EmployeTrainingControllers.cs

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

    public EmployesController(IEmployeService serviceEmploye)
    {

        ServiceEmploye = serviceEmploye;
    }

    //// GET api/employes
    [HttpGet]
    [MyCustomQueryable()]
    public IQueryable<EmployeTraining> Get()
    {

        return ServiceEmploye.GetListeEmployes();
    }
}

要调用我的服务,它只能通过以下URL起作用:https:// {server} / odata / v1 / rh / employetraining

但是我需要使用此https:// {server} / odata / v1 / rh / employe-training请提供任何帮助。

asp.net-core odata
2个回答
0
投票

之所以使用https://{server}/odata/v1/rh/employetraining的原因是因为Get控制器的EmployeTrainingController方法。

如果您将[HttpGet]方法的Get修改为[HttpGet("employe-training")],则应该可以更改此行为


0
投票

对于这种情况,请进行如下更改:

1。更改实体集名称:

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

2。添加属性:

public class EmployeTrainingController : ODataController
{
    [HttpGet]
    [ODataRoute("employe-training")]
    //[MyCustomQueryable()]
    public IQueryable<EmployeTraining> Get()
    {

         return ServiceEmploye.GetListeEmployes();
    }
}

3.Startup.cs:

app.UseMvc(routeBuilder=>
{                
     routeBuilder.Expand().Select().Count().OrderBy().Filter().MaxTop(null);
     routeBuilder.MapODataServiceRoute("EmployeTraining", "odata/v1/rh", EdmModelBuilder.GetEdmModelEmploye());

});

请求网址:https://{server}/odata/v1/rh/employe-training

© www.soinside.com 2019 - 2024. All rights reserved.