none
OData 4 Unbound Functions - Different Service Roots RRS feed

  • Question

  • I would like to have several unbound functions on different service roots.

     public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                config.MapHttpAttributeRoutes();
    
                config.MapODataServiceRoute( "odata1" , "odata/service1" , GetModel1() );
    
                config.MapODataServiceRoute( "odata2" , "odata/service2" , GetModel2() );
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi" ,
                    routeTemplate: "api/{controller}/{id}" ,
                    defaults: new { id = RouteParameter.Optional }
                );
            }
    
            public static IEdmModel GetModel1()
            {
                ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    
                builder.EntitySet<Employee>( "Employees" ).EntityType.HasKey( x => x.Id );
    
                builder.Function( "IsAuthenticated" ).Returns<bool>();
    
                return builder.GetEdmModel();
            }
    
            public static IEdmModel GetModel2()
            {
                ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    
                builder.EntitySet<Job>( "Jobs" ).EntityType.HasKey( x => x.Code );
    
                builder.Function( "TestFunction" ).Returns<string>();
    
                return builder.GetEdmModel();
            }
        }

    But I am receiving the following error when on the application start method in the Global.asax.cs.

    "The path template 'TestFunction' on the action 'TestFunction' in controller 'Jobs' is not a valid OData path template. Resource not found for the segment 'TestFunction'. "

    Here is the stack trace:

    "InvalidOperationException: The path template 'TestFunction' on the action 'TestFunction' in controller 'Jobs' is not a valid OData path template. Resource not found for the segment 'TestFunction'.]
       System.Web.OData.Routing.Conventions.AttributeRoutingConvention.GetODataPathTemplate(String prefix, String pathTemplate, HttpActionDescriptor action) +354
       System.Web.OData.Routing.Conventions.<>c__DisplayClass10.<GetODataPathTemplates>b__d(ODataRouteAttribute route) +23
       System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +164
       System.Linq.WhereEnumerableIterator`1.MoveNext() +170
       System.Web.OData.Routing.Conventions.AttributeRoutingConvention.BuildAttributeMappings(IEnumerable`1 controllers) +413
       System.Web.OData.Routing.Conventions.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) +74
       System.Web.OData.Routing.Conventions.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) +31
       System.Web.Http.HttpConfiguration.EnsureInitialized() +23
       System.Web.Http.GlobalConfiguration.Configure(Action`1 configurationCallback) +43"

    Here are my controllers:

     public class JobsController : ODataController
        {
            private static ODataValidationSettings _validationSettings = new ODataValidationSettings();
    
            public async Task<IHttpActionResult> GetJobs(ODataQueryOptions<Job> queryOptions)
            {
                try
                {
                    queryOptions.Validate(_validationSettings);
                }
                catch (ODataException ex)
                {
                    return BadRequest(ex.Message);
                }
    
                return StatusCode(HttpStatusCode.NotImplemented);
            }
    
            [HttpGet, ODataRoute( "TestFunction" )]
            public IHttpActionResult TestFunction()
            {
                return Ok( "TestFunction" );
            }
    
        }

    public class EmployeesController : ODataController
        {
            private static ODataValidationSettings _validationSettings = new ODataValidationSettings();
    
            public IHttpActionResult GetMyModel1(ODataQueryOptions<Employee> queryOptions)
            {
                try
                {
                    queryOptions.Validate(_validationSettings);
                }
                catch (ODataException ex)
                {
                    return BadRequest(ex.Message);
                }
    
                return Ok( new Employee { Id = 1 } );
            }
    
            [HttpGet, ODataRoute("IsAuthenticated")]
            public IHttpActionResult IsAuthenticated()
            {
                return Ok( true );
            }
        }

    Any help would be greatly appreciated.

    • Moved by CoolDadTx Friday, February 2, 2018 3:12 PM ASP.NET related
    Friday, February 2, 2018 2:28 PM

All replies