【问题标题】:asp.net core 2.1 odata use different name of entity in the routeasp.net core 2.1 odata 在路由中使用不同的实体名称
【发布时间】:2020-06-01 15:37:18
【问题描述】:

我的代码 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 请帮忙。

【问题讨论】:

  • 我试过但没用
  • 您是如何定义[MyCustomQueryable()]的?另外,您的请求网址包含rh,但我看不到您为路由属性或路由模板定义的任何内容。

标签: asp.net-core odata


【解决方案1】:

对于这种情况,改变如下:

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

【讨论】:

【解决方案2】:

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

如果您将Get 方法上的[HttpGet] 修改为[HttpGet("employe-training")],您应该能够更改该行为

【讨论】:

  • 尝试将其更改为[HttpGet("odata/v1/rh/employe-training")],如果可行,那么我们需要查看路由。
猜你喜欢
  • 2019-02-19
  • 2022-01-12
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多