locked
Href value in Url.Link is empty RRS feed

  • Question

  • 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"
                }
            ]
        }
    ]

    Wednesday, April 1, 2020 2:27 PM

All replies

  • User-474980206 posted

    you don't show the code for CreateLinksForTests() but probably it return a new object. in the former case you return the original list, in the latter the new object. try the simpler code:

        var tests = entities.Tests.ToList().Select(t => this.CreateLinksForTests(t));
        return Ok(tests);
    

    Wednesday, April 1, 2020 2:58 PM
  • User-17257777 posted

    Hi .Net Junior,

    Change the Url.Link like this:

    public Test CreateLinksForTests(Test test)
            {
                test.Links.Add(new Link(Url.Link("GetTestById", new { id = test.Id }), "self", "GET"));
                return test;
            }

    Best Regards,

    Jiadong Meng

    Thursday, April 2, 2020 2:31 AM