I am new to asp.net core WebApi and I am trying to read data from a valid json file which is saved inside a folder called mydata inside my webapi
/mydata/userData.json
userData.json:
{
"users": [
{
"id": 1,
"firstName": "John",
"lastName": "Doe"
},
{
"id": 2,
"firstName": "Jane",
"lastName": "Doe"
}
}
I wrote a web method that takes an input as first name, and returns single user object (first found record incase multiple results).
UserController.cs:
[Route("user/{firstName}")]
public User GetUser(string firstName)
{
using (StreamReader r = new StreamReader("~/mydata/userData.json"))
{
string json = r.ReadToEnd();
User item =JsonConvert.DeserializeObject<User>(json);
}
var user = User;
return user;
}
I am facing the following issues:
- I am not able to map the file userData.json, i tried Server.MapPath but it looks like its not available, httpcontext also did not work.
- I do not understand how to return a User Object from this api, which will be used in a react app.