What Was I Thinking?

Follies & Foils of .NET Development
posts - 95 , comments - 352 , trackbacks - 0

WebAPI AttributeRouting Gotcha!

I’m a big fan of the attribute routing in webapi 2.  Its much more descriptive than using an abstracted routing table defined in my webapi config startup.  Recently I had an issue where my controller endpoints where not getting invoked.  Instead, the client requests were returned with a 404 response.

I had this defined in my controller:


1 [RoutePrefix("api/auth/applications")] 2 public class ApplicationController : BusinessServicesControllerBase 3 { 4 /// <summary> 5 /// The method servicing the HTTP GET verb returning a <see cref="Models.Auth.Application"/> . 6 /// </summary> 7 /// <returns><see cref="Models.Auth.Application"/> </returns> 8 [Route("{id}")] 9 [ResponseType(typeof(Models.Auth.Application))] 10 public IHttpActionResult Get(int id) 11 { 12 /// code 13 } 14 }


No matter the route address nor prefix, my routes were not getting hit. UNTIL…

Apparently there is name conflict with my controllers.  I actually have 2 different controllers named ApplicationController, in two different namespaces.  But the routing table doesn’t include the namespace when evaluating routes.  Having duplicate controller names prevented the request from being router to a single controller, so asp.net returned a 404 error.


The answer was simple, I renamed the controller from ApplicationController to ApplicationAuthController (a unique controller name) and my problem was solved.

Print | posted on Wednesday, July 2, 2014 10:21 PM |


No comments posted yet.
Post A Comment

Powered by: