3

Here is my code:

public class SecurityController : ApiController
{
    //GET api/Security/Current
    public HttpResponseMessage GetCurrent(){

    }
    //POST api/Security/Login
    public HttpResponseMessage PostLogin(LoginModel model){

    }
}

public class OrdersController : ApiController
{
    [ActionName("Default")] //GET api/Orders
    public HttpResponseMessage Get(){

    }

    [ActionName("Default")] //GET api/Orders/2
    public HttpResponseMessage Get(long id){

    }

    [ActionName("Default")] //POST api/Orders/
    public HttpResponseMessage Post(Order order){

    }

    [ActionName("Default")] //DELETE api/Orders/2
    public HttpResponseMessage Delete(long id){

    }
    [HttpPost] //POST api/Orders/2/PerformAction
    public HttpResponseMessage PerformAction(long id, ActionMsg action){

    }

}

//Route definitions

        config.Routes.MapHttpRoute("ActionApi", "api/{controller}/{action}");
        config.Routes.MapHttpRoute("WithActionApi", "api/{controller}/{id}/{action}");
        config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional, action = "Default" }, new { id = @"\d+" });

My routing is not working.

No HTTP resource was found that matches the request URI 

What is the correct route definition for my API controller? Thanks!

2
  • What URL are you using? Why are you using [ActionName(Default)]? Commented Jun 19, 2013 at 7:06
  • I was under the assumption that route will map to the action in controller with same action name. That is if I have ActionName attribute with a action then config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional, action = "Default" }, new { id = @"\d+" }); will be used for routing Commented Jun 19, 2013 at 7:51

2 Answers 2

6

Change your SecurityController to:

public class SecurityController : ApiController
{
    //GET api/Security/Current
    [HttpGet]
    [ActionName("current")]
    public HttpResponseMessage GetCurrent(){

    }
    //POST api/Security/Login
    [HttpPost]
    [ActionName("login")]
    public HttpResponseMessage PostLogin(LoginModel model){

    }
}

And then change your routing to:

config.Routes.MapHttpRoute("ActionApi",
  "api/{controller}/{action}",
  null,
  new { action = @"[a-zA-Z]+" });

config.Routes.MapHttpRoute("WithActionApi",
  "api/{controller}/{id}/{action}");

config.Routes.MapHttpRoute("DefaultApi",
  "api/{controller}/{id}",
  new { id = RouteParameter.Optional, action = "Default" },
  new { id = @"\d*" });

Note the regex "\d*" is required in the last route rather than "\d+".

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

Comments

0

Please remove: [ActionName("Default")]

None of your route definitions refer to it. In fact, you don't really need action names at all for web api in general.

With ActionName removed, and with "api/{controller}/{action}" route, you should be able to hit that action by using http://url/api/orders which will call public HttpResponseMessage Get()

1 Comment

Didn't work. Except api/Orders/2/PerformAction all throws 404

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.