4

I want to create some hyperlinks for my DTO`s returned from a REST Api.

The variable url is always null and I do not know why?!

Why is the url not created, what am I still missing?

The name of the route is GetStatusFeedback that's correct and I also use Action and Controller as route parameters + the leadId param!

public class ValuesController : ApiController
{
    [Route("")]
    [HttpGet]
    public IHttpActionResult Get()
    {
        var leadsFromDataBase = new List<Lead> { new Lead { Id = 1 }, new Lead { Id = 2 } };
        var leadDtos = new List<LeadDto>();
        foreach (var lead in leadsFromDataBase)
        {
            var leadDto = new LeadDto();
            string url = Url.Link("GetStatusFeedback", new { LeadId = lead.Id, Action = "Accept", Controller = "values"});
            leadDto.AcceptLink = new Link { Url = url, Verb = "Get" };


            leadDtos.Add(leadDto);
        }
        return Ok(leadDtos);
    }

    [Route("leads/{id:int}/statusfeedback", Name = "GetStatusFeedback")]
    [HttpPost]
    public void Accept(int leadId)
    {

    }
2
  • shouldnt it be Controller = "Values" instead of "values" Commented Aug 9, 2017 at 18:44
  • Nope, did not help! If that would have been the problem I would slap the MS dev for ignoring string.equals with ordinalingore case comparer... Commented Aug 9, 2017 at 18:47

3 Answers 3

6

route parameters need to match the expected parameter in the route template

string url = Url.Link("GetStatusFeedback", new { id = lead.Id });

should match based on the "leads/{id:int}/statusfeedback" route template

also route template placeholders need to match parameter name in the action

[Route("leads/{id:int}/statusfeedback", Name = "GetStatusFeedback")]
[HttpPost]
public void Accept(int id) {

}

Finally you should use proper route names that help describe the route.

Sign up to request clarification or add additional context in comments.

1 Comment

argh... I stepped into that old error it happened before :P That was it! To improve the code stability you still can nameof(Accept) the routeName ;-)
0

It took me a while to get what I needed from the accepted answer (and my mistake differed from the OP), here is a simplified version based on Nkosi's answer and OP's comment:

public class ValuesController : ApiController
{
  [HttpGet]
  public IHttpActionResult Get()
  {
    string url = Url.Link("GetStatusFeedback", new { Id = 1, Action = "Accept", Controller = "values"});
    /*or*/ url = Url.Link(nameof(Accept), new { Id = 1,      Action = "Accept", Controller = "values"});
    //not  url = Url.Link(nameof(Accept), new { LeadId = 1,  Action = "Accept", Controller = "values"});

    return Ok(url);
  }

// Controller we want to get the URL of:
[HttpPost("leads/{id:int}/statusfeedback", Name = "GetStatusFeedback")]
public void Accept(int id) //param name matches the line above {id} (in OP it mismatched)
{

}

Comments

0

My problem was the router name parameter, I was using the controller name but is the router registered on Program.cs

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

for my default was the expected name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.