1

So I thought this would be easy.. ahh.

I suspect its no different to 2.2 but for some reason I cannot get the route to match and accept a query string. I dont know if it has to do with new Endpoint routing etc so in the interests of others and of course myself I have put a question up even though this is suppose to be simple.

I did look at this question and though its similar it deals with an id. I have a query and the form coming back from the javascript program at the other end is of the format:

http://localhost:57000/api/Client/Index?$top=10&$orderby=ClientNo%20asc,ClientLastName%20asc,MobilePhone%20asc

I get the following back:

aurelia-fetch-client.js:199 GET http://localhost:57000/api/Client/Index?$top=10&$orderby=ClientNo%20asc,ClientLastName%20asc,MobilePhone%20asc 404 (Not Found)

My startup.cs is as follows:

        app.UseRouting();
        app.UseEndpoints(endpoints => {
            endpoints.MapControllers();

        app.UseAuthentication();
        app.UseStaticFiles();

        app.UseHttpsRedirection();
        app.UseFileServer();

        app.UseSession();
        app.UseAuthorization();
        app.UseCors();
        app.UseConfigureSession();

So I have "UseEndpoints" instead of the "app.UseMvc.." and I am not sure if this changes the way I should structure my controller.

My controller has the following:

[Microsoft.AspNetCore.Components.Route("api/[controller]")]
public class ClientController : Controller {

and the controller method:

    [HttpGet(template: "index{query}", Name = "ClientIndex1")]
    public IActionResult Index(string query) {
        var clientList = _clientServices.GetClients();

        return new OkObjectResult(clientList);
    }

So what am I doing wrong with the query and is there other issues at hand etc?

si2030
  • 3,895
  • 8
  • 38
  • 87

1 Answers1

0

I think the problem is with your route template "index{query}". The Http request url is "http://localhost:57000/api/Client/Index" everything after the ? is being parsed as parameters.

I would write something like this

[HttpGet(template: "index", Name = "ClientIndex1")]
public IActionResult Index() {
    var clientList = _clientServices.GetClients();
    // use Request.Query["$top"] to access the odata parameters.
    return new OkObjectResult(clientList);
}
verbedr
  • 1,784
  • 1
  • 15
  • 18