3

我创建了一个新的ASP.NET MVC4 Web Api项目。除了默认ValuesController之外,我还添加了另一个控制器,ScenarioController。它具有与ValuesController完全相同的方法。但由于某种原因,它的行为有所不同。ASP.NET MVC4 Web API控制器停留在一条路线上

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

使用断点,我知道,其实/api/scenario/1获取发送到public IEnumerable<string> Get(),而不是预期的public string Get(int id)。为什么?

仅供参考,这里有相关的文件(这些都是原始默认mvc4-的WebAPI类,没有修改任何东西):

的Global.asax.cs

namespace RoutingTest 
{ 
    // Note: For 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 the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type. 
      // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries. 
      // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712. 
      //config.EnableQuerySupport(); 

      // To disable tracing in your application, please comment out or remove the following line of code 
      // For more information, refer to: http://www.asp.net/web-api 
      config.EnableSystemDiagnosticsTracing(); 
     } 
    } 
} 

ValuesController.cs

namespace RoutingTest.Controllers 
{ 
    public class ValuesController : ApiController 
    { 
     // GET api/values 
     public IEnumerable<string> Get() 
     { 
      return new string[] { "value1", "value2" }; 
     } 
     // GET api/values/5 
     public string Get(int id) 
     { 
      return "value"; 
     } 
    } 
} 

ScenarioController.cs(是的,它在Controllers文件夹)

namespace RoutingTest.Controllers 
{ 
    public class ScenarioController : ApiController 
    { 
     // GET api/scenario 
     public IEnumerable<string> Get() 
     { 
      return new string[] { "value1", "value2" }; 
     } 

     // GET api/scenario/5 
     public string Get(int id) 
     { 
      return "value"; 
     } 
    } 
} 

回答

2

Gremlins。感谢@Pete Klien验证代码在我的机器外部工作。这就是我所做的。

  1. 经验丰富的问题​​控制器只使用1方法获取原始项目。
  2. 创建新的Web Api项目,并在问题中发布了代码。同样的症状。
  3. 清洁工程,重建所有,仍然没有骰子。
  4. 重新启动机器,清理,重建,再试一次,没有骰子。
  5. 创建新的Web Api项目新解决方案,成功!
+0

+1发布最终解决这个问题的方法 – 2013-05-10 13:57:36

+0

... aaaaaand,问题又回来了,我一直在设置新的解决方案e代码&配置从非工作项目,并在测试第一个(新增加的)控制器时,发现只使用未参数化的Get方法相同的问题! – Noel 2013-05-10 20:30:36

+1

welp,它有助于保持您的控制器方法参数名称和路由配置参数名称相同,孩子。 – Noel 2013-05-10 21:07:50

2

我想你的代码只是现在得到了预期的结果:

> curl http://localhost:53803/api/values 
["value1","value2"] 
> curl http://localhost:53803/api/values/1 
"value" 
> curl http://localhost:53803/api/scenario 
["value1","value2"] 
> curl http://localhost:53803/api/scenario/1 
"value" 
> 

(通过方式,没有要求它在Controllers文件夹。 HttpConfiguration.Routes.MapHttpRoute可以简单地认为所有的类,从ApiController继承。)

我不是在讽刺我建议你所有重建,然后再试一次。

+0

谢谢你试图欺骗它。是的,我做了一次重启,并且创建了新项目来复制我在另一个项目中看到的内容,并且在做同样的事情时感到很惊讶。许多清洁项目和重建所有已完成:( – Noel 2013-05-10 02:23:40

+0

我没有经验的asp.net mvc - 是否有任何其他可以影响配置,这可能会发生?NuGet包?NET 4.0 vs 4.5? – Noel 2013-05-10 02:37:43

+0

刚刚创建了一个新的*解决方案*,使用相同的代码,并且工作正常。 – Noel 2013-05-10 03:44:43

0

我有这个问题,并没有得到任何工作。最后,我更改了IIS Express Project Url设置上的端口,并且全部恢复正常。它是本地主机:57846。我只是把它变成localhost:57847,一切恢复正常。