Home > Software engineering >  EF Core API HttpGet parameter null
EF Core API HttpGet parameter null

Time:08-15

I am developing an EF Core Web API. When I query with FirstName, the variable gets null. Where am I doing wrong?

[HttpGet]
public IList<User> Get()
{
    var urunler = from Users in myDbContext.Users select Users;
    return urunler.ToList();
}

[HttpGet("FirstName")]
public IList<User> Get(string name)
{
    var urunler = from Users in myDbContext.Users where Users.FirstName == name select Users;
    var result = urunler.ToList();
    return result;
}

CodePudding user response:

may be you should use string functions like Contains(),ToLower().I think its case sensitive issue because == operator check exactly same value on both side. if i misunderstood your issue so explain your issue more.

CodePudding user response:

The reason why your name parameter is null is because the framework does not know where to populate the name parameter from.

if you change your code to this:

[HttpGet]
public IList<User> Get()
{
    var urunler = from Users in myDbContext.Users select Users;
    return urunler.ToList();
}


[HttpGet("{name}")]
public IList<User> Get(string name)
{
    var urunler = from Users in myDbContext.Users where Users.FirstName == name select Users;
    var result = urunler.ToList();
    return result;
}

it should work

this will allow you to use the route

/firstName

the query will then return all people who have the Users.FirstName = "firstName"

CodePudding user response:

If your name variable is not empty and if you have same data in your database, you should look a different steps. This code is should work I can't see any problem.

You can open profiler and check your SQL query for find the problem and You should check your entites end mappings. Problem is should be there.

  • Related