Nasreddine
Nasreddine

Reputation: 33

asp.net core 2.1 odata use different name of entity in the route

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.

Upvotes: 0

Views: 363

Answers (2)

Rena
Rena

Reputation: 36655

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

Upvotes: 1

TiGreX
TiGreX

Reputation: 1816

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")]

Upvotes: 0

Related Questions