Search code examples
c#.netodata

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


I have been Googling and using Chat GPT and cannot figure out how to get rid of the above warning. Here is my code:

var builder = new ODataConventionModelBuilder();
builder.EntitySet<MessageViewModel>("Message");

// Define the bound action
// var getTagMessagesAction = builder.EntityType<MessageViewModel>().Collection.Action("GetTagMessages");

// Optionally define parameters for the action
// getTagMessagesAction.Parameter<List<int>>("tagIds");
[Authorize]
[Route("odata")]
public class MessageController : ODataController
{
[HttpPut]
[Route("GetTagMessages")]
public async Task<IActionResult> GetTagMessages([FromBody] List<int> tagIds)
{
    var messages = await _db.MessageTags.Where(x => tagIds.Any(t => t == x.TagId))
        .AsNoTracking().ToListAsync();
    var mapped = _mapper.Map<List<Message>, List<MessageViewModel>>(messages);
    return Ok(mapped);
}

}

I am calling it with https://localhost:5001/odata/GetTagMessages and passing a list of integers in the body.

The call is successful without configuring the action in the EDM model builder, but I am getting the warning when I run the solution:

warn: Microsoft.AspNetCore.OData.Routing.Conventions.AttributeRoutingConvention[0] The path template 'odata/GetTagMessages' on the action 'GetTagMessages' in controller 'Message' is not a valid OData path template. Resource not found for the segment 'GetTagMessages'.

When I configure the action as I have in the EDM Model Builder, by removing the commented code, I get 405 Method Not Allowed. I try changing to a POST call and still get the same error.

If I remove [Route("odata")] from the controller, and change the route on the endpoint to [Route("~/GetTagMessages")], the warning goes away, but I get the 405 error.

I'm not sure what I am doing wrong, and any help would be appreciated. I'm trying to get a clean build without the warning so that everything is properly configured.


Solution

  • The warning message is expected based on your model schema and controller/action setting. Because, there's no element called 'GetTagMessages' in your model.

    If you config the action in the Edm model builder, you get "405" is expected because OData routing builds a 'conventional' endpoint for 'GetTagMessage' controller method.

    I create a sample for your reference and make the Edm action working without warning. See details at commit https://github.com/xuzhg/WebApiSample/commit/87cfed8981156ab2edde5618cb9f28eb4e6fc057

    Please let me know your details requirements. You can file issue on the github or leave the comments here.

    Thanks.