1

After developing a simple test ASP.Net WebApi solution to implement Unity interface DI into my Controller I have hit an issue with getting my API to route to the relevant methods correctly.

Typing in the following URL will return the first GET method as expected:

http://localhost:1035/api/values

Typing in a parameter to hit the GetSelectedPerson method in the controller is never registered:

http://localhost:1035/api/values/Test

Hopefully someone can tell me where I'm going wrong, heres the relevant code.

RouteConfig from the App_Start folder:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Here is the WebApi config again from the App_Start folder:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new
            {
                id = RouteParameter.Optional
            }
        );

        config.Formatters.Remove(config.Formatters.XmlFormatter);
    }
}

Here are the two GET HTTP methods I've implemented within the 'Values' controller:

public class ValuesController : ApiController
{

    private IPersonCreator _createPerson;

    public ValuesController(IPersonCreator createPerson)
    {
        _createPerson = createPerson;
    }

    //GET api/values
    public IPerson Get()
    {
        return _createPerson.CreateNewPerson();
    }

    //**********Issue: This Method is never hit.**********
    public IPerson GetSelectedPerson(string nameOfPerson)
    {
        IPerson selectedPerson = null;

        var returnedPeople = _createPerson.CreateNewPeople();

        foreach (var person in returnedPeople)
        {
            if (person.Name == "John")
            {
                selectedPerson = person;
            }
        }

        return selectedPerson;
    }
0

1 Answer 1

1

This is a parameter binding problem. In the default route the expected parameter name is id, however, in your action you have nameOfPerson.

You have two options here, you can either rename your nameOfPerson parameter to be id i.e.

public IPerson GetSelectedPerson(string id) 

or alternatively add a specific route which expects a nameOfPerson parameter i.e.

// place after default route
config.Routes.MapHttpRoute(
    name: "PersonByNameApi",
    routeTemplate: "api/{controller}/{nameOfPerson}",
    defaults: new
    {
        nameOfPerson = RouteParameter.Optional
    }
);
Sign up to request clarification or add additional context in comments.

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.