1

I have a problem that I want to call a MVC Api method with a custom name.

I changed the WebApi.config as described here

    config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id="test" }
    );

and wrote a class

public class MissingCardBoxModelController : ApiController
{
    // GET api/missingcardboxmodel
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/missingcardboxmodel/5
    public string Get(string id)
    {
        return id;
    }

    public string GetTrackingNumber(string parcelLabelNumber) 
    {
        string trackingNumber = "some number";
        return trackingNumber;
    }

    // POST api/missingcardboxmodel
    public void Post([FromBody]string value)
    {
    }

    // PUT api/missingcardboxmodel/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/missingcardboxmodel/5
    public void Delete(int id)
    {
    }
}

But I can't call the method via http://localhost:58528/api/MissingCardBoxModel/GetTrackingNumber/123456

I Get the message

No action was found on the controller 'MissingCardBoxModel' that matches the request.

Why can't I call the method ?

4
  • what web api version are you using? Commented Jan 26, 2016 at 8:59
  • 3
    Try changing parcelLabelNumber in your arguments to id. Commented Jan 26, 2016 at 9:00
  • I use the Webapi Controller v1 Commented Jan 26, 2016 at 9:00
  • @BrendanGreen Okay that solved the problem. I see where the problem is. I thought that the variable name id is just a placeholder. Commented Jan 26, 2016 at 9:02

2 Answers 2

3

If your routes are configured to be these (default in the MVC solution template):

url: "{controller}/{action}/{id}"

You should change parcelLabelNumber to id.

You can read more about routes here.

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

Comments

0

By Default Web API allows Restful conventions that means it will auto map GET, PUT, POST, DELETE etc action names. if you look inside your WebApiConfig in routes it only allows the route below

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

which means it only allows

.../api/yourcontrollername/a parameter that will map to id

. you basically have 2 options, one to use attribute routing. or you can add a route to your custom method eg:

config.Routes.MapHttpRoute(
                name: "custom",
                routeTemplate: "api/{controller}/{action}/{parcelLabelNumber}",
                defaults: new { parcelLabelNumber = "" }
            );

please also notice the parameter name here "parcelLabelNumber", you have to name your parameter same here as in your action. You should be able to reach this action at - http://localhost:23691/api/MissingCardBoxModel/GetTrackingNumber/1245

Also please have a look at Routing in general

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.