1

I have got implemented console self-hosted WebAPI app.

My controller works fine for single get and post methods.

I cannot get how to implementg multiple get and posts methods in ApiController

Startup.cs

 class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Initialize WebAPI
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "apiRoute",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            app.UseWebApi(config);

            // Initialize SignalR
            app.UseCors(CorsOptions.AllowAll);
            app.MapSignalR(); 
        } 
    }

ApiController that works fine

public class MessageController : ApiControllerWithHub<SignalMessageHub>
{
   public IEnumerable<MyItem> GetMessages()
   {
      //Code
   }

   public HttpResponseMessage PostNewMessage(User user)
   {
      // code  
   }
}

ApiController that does not work

public class MessageController : ApiControllerWithHub<SignalMessageHub>
{
   public IEnumerable<MyItem> GetMessages()
   {
      //Code
   }

   // Cannot route to those methods 
   [HttpPost()]
   [Route("api/Message/NewMessage")]
   public HttpResponseMessage PostNewMessage(User user)
   {
      // code  
   }

   [HttpPost()]
   [Route("api/Message/NewMessage2")]
   public HttpResponseMessage PostNewMessage2(User user)
   {
      // code  
   }

}
2
  • 2
    Try changing your routing from this routeTemplate: "api/{controller}/{id}", to routeTemplate: "api/{controller}/{action}/{id}", and remove api/Message from the Actions Route then you should be able to get them on "api/Message/NewMessage" Commented Jul 25, 2017 at 6:52
  • 1
    @Dimi Sorry, posted before the edit! Samvel's answer looks good. Commented Jul 25, 2017 at 6:56

1 Answer 1

4

Your problem is with your Routing. Here

routeTemplate: "api/{controller}/{id}",

You have not added "{action}" so you can't get Actions until you change this to this:

routeTemplate: "api/{controller}/{action}/{id}",

Also you have added "api/Message" to the Action's routing in the controller which will make your routing to something like:

api/{controller}/{action}/api/Message/NewMessage

So you need to remove it also.

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.