0

I'm having issues navigating to my ListPeople method using the URL. If I type localhost:12345/People?peopleId=405&age=24 into my URL it works fine; however, when I pass localhost:12345/People?peopleId=405 into my URL, it returns a 404 error.

404 Error Message:

"No HTTP resource was found that matches the request URI'http://localhost:12345/People?peopleId=405'."

How can I change the routing of my ListPeople controller so that it excepts BOTH url's?

Controller

[HttpGet]
[ReturnDescription("List of People")]
[CustomActionName("")]
[Description("List of people  for given peopleId and age")]
public ApiResponse<Status, IEnumerable<People>> ListPeople(int peopleId, int? age)
{
    return _personProject.ListPeople(peopleId, age);
}

Successful URL

localhost:12345/People?peopleId=405&age=24

Failing URL

localhost:12345/People?peopleId=405

2 Answers 2

1

As it is written, you have a method that represents the url:

/People?peopleId={0}&age={0}

You have to make an overload that will only accept peopleId because a method can only be the target of one route when you're setting it up automatically like this. What I would advise would be to make a simple overload:

public ApiResponse<Status, IEnumerable<People>> ListPeople(int peopleID)
{
    return ListPeople(peopleId, null);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Sorry i can't write comment.So I must write here.If you'r using api Check your WebApiConfig. You must define your parameters in this config.

Like;

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{peopleid}/{age}",
            defaults: new { peopleid = RouteParameter.Optional, age = RouteParameter.Optional }
        );

Just an example. I think i can help you.

1 Comment

Thanks for your help @CoderWho. I ended up using the answer above, but gave you an up arrow! :)

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.