0

I am getting really weird routing issues when calling actions in my APIController. I have a WebApp, but needed an APIController so I added that as well as WebAPIConfig.cs to App_start and Global.asax.

However, when I try to call different Actions inside the APIController, it seems to not differentiate between the Actions unless I add a parameter. For example, if I call api/controller/happy it enters the same Action as api/controller/sad. It enters the Action that was created first in the Controller.

It makes no sense to me, the Action-names are not being considered in the URL.

my API Controller:

public class RegistrationManagerController : ApiController
    {
        EventHelper eh = new EventHelper();

        [HttpGet]
        public IHttpActionResult IsUserRegistered(string skypeid)
        {
            var skypeuser = Exist.CheckIfRegistered(skypeid);

            return Ok(skypeuser);
        }

        [HttpGet]
        public async Task<IHttpActionResult> Happy()
        {
            var events = await eh.GetHappyRecent();

            return Ok(events);
        }

        [HttpGet]
        public async Task<IHttpActionResult> Sad()
        {
            var events = await eh.GetSadRecent();

            return Ok(events);
        }

        [HttpPost]
        public async Task<IHttpActionResult> UpdateEvent() //TODO id send in body?
        {
            await eh.Update("id");

            return Ok();
        }
    }

My WebAPIConfig.cs:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

My RouteConfig (This is a webapp, not a web API):

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapMvcAttributeRoutes();// New feature
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
2
  • See here, looks like your exact problem stackoverflow.com/questions/10121152/… Commented Feb 14, 2017 at 15:53
  • 1
    your routetemplate does not contain the action, just the controller Commented Feb 14, 2017 at 15:54

2 Answers 2

2

Routetemplate should be

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

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

Comments

0

WebApi follows REST principals so it doesn't route the same way an MVC Controller routes.

Check out this answer here I think it will help you

need route for my web api 2 controller

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.