Search code examples
c#restasp.net-web-api2odata

OData path template is not a valid OData path template


I have a HttpGet method that has the ODataRoute

["Users({userId}/Tags)"]

userId is a string and the method name is UserTags. Controller is UsersController.

When I run the app I get the following error:

The path template Users({userId})/Tags on the action 'UserTags' in controller Users is not a valid OData path template. Found an unresolved path segment Tags in the OData path template Users({userId})/Tags.


Solution

  • The constraints for ODataRoute are pretty strict, your user entity must have a collection property called 'Tags' for your route to work.

    With the following code I got it to work without errors:

    public class UserController : ODataController
    {
        [HttpGet]
        [System.Web.OData.Routing.ODataRoute("User({userId})/Tags")]
        public IHttpActionResult GetTags([FromODataUri]int userId)
        {
            //...
        }
    }
    
    public class User
    {
        [Key]
        public int Id { get; set; }
        public List<Tag> Tags { get; set; }
    }