3

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

/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 类,没有修改任何东西):

全球.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();
        }
    }
}

值控制器.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";
        }
    }
}
4

3 回答 3

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。)

当我建议您重建所有并重试时,我并不是在讽刺。

于 2013-05-09T23:21:19.280 回答
2

小精灵。感谢@Pete Klien 验证代码在我的机器之外是否有效。这就是我所做的。

  1. Controller在原始项目中仅使用一种方法获取的经验问题。
  2. 使用我在问题中发布的代码创建了新的 Web Api 项目。一样的症状。
  3. 清理项目,重建全部,仍然没有骰子。
  4. 重新启动机器,清理,重建,再试一次,没有骰子。
  5. 在新的解决方案中创建新的 Web Api 项目,成功!
于 2013-05-10T13:23:17.353 回答
0

我遇到了这个问题,无法进行任何工作。最后,我更改了 IIS Express Project Url 设置上的端口,一切恢复正常。它是本地主机:57846。我刚刚将其设为 localhost:57847,一切都恢复正常。

于 2018-02-28T15:25:56.433 回答