-1

I'm trying to query for the "UserType" object, and get the attribute "name" So the whole JSON object is User: and the api call should look like this: api/user?userTypeName=randomUserTypeName

Where I can search for a specific userTypeName, and return the name.

API call:

Here I want to user the FromQuery, to be able to search for the userTypeName and check for the userTypeName to be equal to the entered Name.

// GET: /api/User/userTypeName
[HttpGet("{userTypeName}")]
public async Task<ActionResult<IEnumerable<User>>> GetAsync(([FromQuery(Name = "userTypeName")]string userTypeName) {
    var result = await ctx.User
        .Include(x => x.UserType)
        .Where(x => x.UserType.Name.ToLower() == UserTypeName.ToLower())
        .ToListAsync();

    if (result == null) return BadRequest();

    return result;
}

The object looks like this:

{
    "id": 9,
    "userTypeId": 1,
    "userType": {
    "id": 1,
    "name": "randomUserTypeName",
    "users": 
    [
        {
            "id": 11,
            "userTypeId": 1,
            "userUrl": "https://userurl",
            "username": "eee",
            "password": "eee",
        }
    ]
}
xdtTransform
  • 1,986
  • 14
  • 34
Langehk
  • 1
  • 2

1 Answers1

0

If you want to access userTypeName from Route by request like /api/User/userTypeName, your action should like

[HttpGet("{userTypeName}")]
public async Task<ActionResult<IEnumerable<User>>> GetAsync(string userTypeName) {
    var result = await ctx.User
        .Include(x => x.UserType)
        .Where(x => x.UserType.Name.ToLower() == UserTypeName.ToLower())
        .ToListAsync();

    if (result == null) return BadRequest();

    return result;
}

If you want to access userTypeName from Query by request like api/user?userTypeName=randomUserTypeName, your action should like

[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetAsync([FromQuery(Name = "userTypeName")]string userTypeName) {
    var result = await ctx.User
        .Include(x => x.UserType)
        .Where(x => x.UserType.Name.ToLower() == UserTypeName.ToLower())
        .ToListAsync();

    if (result == null) return BadRequest();

    return result;
}
Edward
  • 28,296
  • 11
  • 76
  • 121
  • It won't let me use [HttpGet] without defining the ("{userTypeName}") attribute. I already have a GetAsync method, for only getting the Users. Does this conflict somehow? – Langehk Jul 30 '19 at 09:46
  • @Langehk What is the reason you used `{userTypeName}` and `FromQuery`? – Edward Jul 31 '19 at 01:20
  • I'm just trying to query for the userTypeName. And if I don't use {userTypeName} it gives me an error. – Langehk Aug 01 '19 at 09:06
  • An unhandled exception occurred while processing the request. AmbiguousMatchException: The request matched multiple endpoints. Matches: ` Proj.Presentation.MVC.API.UserController.GetAsync (Proj.Presentation.MVC) Proj.Presentation.MVC.API.UserController.GetAsync (proj.Presentation.MVC) /_Host ` – Langehk Aug 01 '19 at 11:19
  • @Langehk This is caused by you are using the `HttpGet` for multiple action. Change `[HttpGet("{userTypeName}")]` to `[HttpGet("[action]")]`. – Edward Aug 02 '19 at 01:26