asp.net 核心 2.1 OData 在路由中使用不同的实体名称



我的代码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 请提供任何帮助。

对于这种情况,请像下面这样更改:

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.启动.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

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

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

最新更新