I work on my web api project.
I have two get action methods in controller.
Here the controller:
namespace Playground.Web.Controllers.API { [RoutePrefix("api/DamageEvent/{actionType}")] public class DamageEventController : ApiController { #region API methods [HttpGet] public async Task<IHttpActionResult> GetDamageEvent(int damageEventId = 0) { //some logic } [HttpGet] [Route("{ddd:int}")] public async Task<IHttpActionResult> GetDamageEvent2(int ddd = 0) { //some logic } #endregion } } Here WebApiConfig defenition:
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 } ); config.Formatters.JsonFormatter.SerializerSettings.DateFormatString = "dd/MM/yyyy"; } } Here the example of URL in fiddler compose to trigger web api action:
http://localhost/playground/api/DamageEvent/GetDamageEvent2/?ddd=22 I expect that for the URL above the GetDamageEvent2 web api action will be fired. But instead GetDamageEvent action method is fired.
Why GetDamageEvent2 not fired? Any idea what do I am missing?
==============================Update================================
After I red answer from Nkosi
I made some changes to my code, I added to class WebApiConfig new route:
config.Routes.MapHttpRoute( name: "ActionApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); And here the changes in action type:
namespace Playground.Web.Controllers.API { [RoutePrefix("api/DamageEvent")] public class DamageEventController : ApiController { #region API methods [HttpGet] [Route("GetDamageEvent/{damageEventId}")] public async Task<IHttpActionResult> GetDamageEvent(int damageEventId = 0) { //some logic } [HttpGet] [Route("GetDamageEvent2/{ddd}")] public async Task<IHttpActionResult> GetDamageEvent2(int ddd = 0) { //some logic } #endregion } } After I make the changes above the I tryed to fire the both actions and it worked.
But the problem now is when I try to call another actions in another controllers, For example:
http://localhost/playground/api/Contracts/1 I get 404 error.
So I guess the error occures because of the new route template.
So my question how can I fix the error above and to take the new route template into consideration only when the URI try to access to DamageEventController?
DamageEventController? If not then you can safely remove that route and keep your default asDamageEventControlleris fully using attribute routing.