0
votes

I have a long name of of entity in my code EmployeTraining which used as entity in OData and with same name for the controller.

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();
    }
}

To call my service it works only through this URL: https://{server}/odata/v1/rh/employetraining

but I need to use this https://{server}/odata/v1/rh/employe-training any help please.

2
I tried but did not work - Nasreddine
How did you define [MyCustomQueryable()]?Also,your request url contains rh,but I could not see anything you define for the route attribute or route template. - Rena

2 Answers

1
votes

For such scenario,change like below:

1.Change the entityset name:

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

2.Add the attribute:

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());

});

Request the url:https://{server}/odata/v1/rh/employe-training

0
votes

The Reason why is working using https://{server}/odata/v1/rh/employetraining is because is the Get method of the EmployeTrainingController Controller.

You should be able to change that behaibour if you modify the [HttpGet] on the Get method to [HttpGet("employe-training")]