How to do custom function routing in C# / ASP.NET Core 6 Web API Server OData controller?

Viewed 311

I am trying to implement a custom function in my OData based controller. This function should create a number of test records in the database. The number of test records to be created is passed as parameter. However, I am totally clueless on how to specify the route to this function so that calling it on the client side would actually end up in the function I have implemented in the controller on the server.

public class DocumentsController : ODataController
{
    private readonly DS2DbContext _db;
    private readonly ILogger<DocumentsController> _logger;

    [EnableQuery]
    [HttpGet("Documents/CreateTestDocuments(count={count})")]
    public IActionResult CreateTestDocuments([FromODataUri] int count)
    {
        // ...
    }
}

All my other OData code is just basic vanilla configuration to call the usual basic entity/property query, creation / patch / deletion controller methods.

I am aware that there is a similar question, but it is two years old and has no answers.

1 Answers

I tried with the codes and worked:

[ApiController]
    
    public class DocumentController : ODataController
    {
       
        private List<Document> documents = new List<Document>()
        {
            new Document()
            {
                Id = 1,
                Name = "Doc1",
            }
        };
        public DocumentController()
        {
            
        }
        
        [HttpGet]
        [EnableQuery]
        [Route("[controller]/CreateTestDocument({key})")]
        public IActionResult CreateTestDocument([FromODataUri] int key)
        {
            
            return Ok();
        }
    }

enter image description here

Related