User-111502429 posted
So I have these two methods in my controller
[Route("api/Tests", Name = "GetAllTests")]
public IHttpActionResult Get()
{
using (TDBEntities entities = new TDBEntities())
{
List<Test> test= entities.Tests.ToList();
foreach (Test t in test)
{
this.CreateLinksForTests(t);
}
return Ok(test);
}
}
[Route("api/Tests/{id}", Name = "GetTestById")]
public IHttpActionResult Get(int id)
{
using (TDBEntities entities = new TDBEntities())
{
Test test = entities.Tests.FirstOrDefault(t => t.Id == id);
if (product != null)
{
return Ok(this.CreateLinksForTests(test));
}
else
{
return Content(HttpStatusCode.NotFound,"not found");
}
}
}
public Test CreateLinksForTests(Test test)
{
test.Links.Add(new Link(Url.Link("GetTestById", test.Id), "self", "GET"));
return test;
}
When i call GetTestById in postman, the Href value is filled
{
"Id": 3,
"Name": "name3",
"Links": [
{
"Href": "https://localhost:11111/api/Tests/3",
"Rel": "self",
"Method": "GET"
}
]
}
But when i call GetAllTests in postman, the href value is always null which means the problem is in my GetAllTests fucntion and not in createLinksForTests. Because as you can see in the example above the href was filled with the link but
now it's null. How can i solve this?
[
{
"Id": 1,
"Name": "name1",
"Links": [
{
"Href": null,
"Rel": "self",
"Method": "GET"
}
]
},
{
"Id": 2,
"Name": "name2",
"Links": [
{
"Href": null,
"Rel": "self",
"Method": "GET"
}
]
},
{
"Id": 3,
"Name": "name3",
"Links": [
{
"Href": null,
"Rel": "self",
"Method": "GET"
}
]
}
]