Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core, Web API RouteAttribute with query string as template

I need to make a ASP.NET Core, Web API that supports multiple HttpGet verbs with the only difference being a query string, but it seems that query strings cannot be part of the route template -- is that true?

The route templates are very similar, in fact they only differ by query string.

[Authorize]
public class SymbolsController : Controller
{
    [
        HttpGet, 
        Route("api/symbols")
    ]
    public Task<IEnumerable<Symbol>> Symbols([FromServices] ISymbolService symbolService)
    {
        return symbolService.GetSymbolsAsync();
    }

    [
        HttpGet, 
        Route("api/symbols?{childrenOf=id}")
    ]
    public Task<IEnumerable<Symbol>> ValidChildren(
        [FromQuery] Guid id, 
        [FromServices] ISymbolService symbolService)
    {
        return symbolService.GetValidChildrenAsync(id);
    }
}

This throws an exception as the ? is not a valid character in the route template. How can I achieve this?

like image 236
David Pine Avatar asked Aug 16 '16 14:08

David Pine


2 Answers

I figured out an easier and more straightforward solution. I am simply using the FromQuery attribute and leaving the template simple. I check for the presence of the id variable and handle it accordingly.

[HttpGet, Route("api/symbols")]
public Task<IEnumerable<Symbol>> Symbols(
    [FromQuery] Guid id, 
    [FromServices] ISymbolService symbolService) => 
    id == default
        ? symbolService.GetSymbolsAsync()
        : symbolService.GetValidChildrenAsync(id);
like image 155
David Pine Avatar answered Nov 19 '22 19:11

David Pine


//eg GET api/symbols
//eg GET api/symbols?childrenOf={someGuid}
[HttpGet("api/symbols")]
public Task<IEnumerable<Symbol>> Symbols(
    [FromServices] ISymbolService symbolService, 
    Guid? childrenOf = null)
{
    if (childredOf.HasValue) {
        return ValidChildren(childredOf.Value, symbolService);
    }
    return symbolService.GetSymbolsAsync();
}

Task<IEnumerable<Symbol>> ValidChildren(
    Guid id, 
    ISymbolService symbolService)
{
    return symbolService.GetValidChildrenAsync(id);
}
like image 1
Nkosi Avatar answered Nov 19 '22 18:11

Nkosi