c# - ASP.NET MVC4 Web API Controller Stuck on One Route -


i created new asp.net mvc4 web api project. in addition default valuescontroller, added controller, scenariocontroller. has exact same methods valuescontroller. reason, behaves differently.

/api/values/ => "value1","value2" /api/values/1 => "value" /api/scenario/ => "value1","value2" /api/scenario/1 => "value1","value2"                    ^^^^^^^^^^^^^^^^^                    should return "value"! 

using breakpoints, know /api/scenario/1 gets sent public ienumerable<string> get(), not expected public string get(int id). why?

for reference, here relevant files (these pristine default mvc4-webapi classes, haven't modified anything):

global.asax.cs

namespace routingtest {     // note: instructions on enabling iis6 or iis7 classic mode,      // visit http://go.microsoft.com/?linkid=9394801      public class webapiapplication : system.web.httpapplication     {         protected void application_start()         {             arearegistration.registerallareas();              webapiconfig.register(globalconfiguration.configuration);             filterconfig.registerglobalfilters(globalfilters.filters);             routeconfig.registerroutes(routetable.routes);             bundleconfig.registerbundles(bundletable.bundles);         }     } } 

webapiconfig.cs

namespace routingtest {     public static class webapiconfig     {         public static void register(httpconfiguration config)         {             config.routes.maphttproute(                 name: "defaultapi",                 routetemplate: "api/{controller}/{id}",                 defaults: new { id = routeparameter.optional }             );              // uncomment following line of code enable query support actions iqueryable or iqueryable<t> return type.             // avoid processing unexpected or malicious queries, use validation settings on queryableattribute validate incoming queries.             // more information, visit http://go.microsoft.com/fwlink/?linkid=279712.             //config.enablequerysupport();              // disable tracing in application, please comment out or remove following line of code             // more information, refer to: http://www.asp.net/web-api             config.enablesystemdiagnosticstracing();         }     } } 

valuescontroller.cs

namespace routingtest.controllers {     public class valuescontroller : apicontroller     {         // api/values         public ienumerable<string> get()         {             return new string[] { "value1", "value2" };         }         // api/values/5         public string get(int id)         {             return "value";         }     } } 

scenariocontroller.cs (yes, it's in controllers folder)

namespace routingtest.controllers {     public class scenariocontroller : apicontroller     {         // api/scenario         public ienumerable<string> get()         {             return new string[] { "value1", "value2" };         }          // api/scenario/5         public string get(int id)         {             return "value";         }     } } 

gremlins. @pete klien verifying code work outside machine. here's did.

  1. experienced problem of controller using 1 method in original project.
  2. created new web api project, code posted in question. same symptom.
  3. clean project, rebuild all, still no dice.
  4. reboot machine, clean, rebuild, try again, no dice.
  5. create new web api project in new solution, success!

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -