Reputation: 3661
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 }
);
}
}
Upvotes: 0
Views: 215
Reputation: 1101
Routetemplate should be
routeTemplate: "api/{controller}/{action}/{id}"
Upvotes: 2
Reputation: 1809
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
Upvotes: 0