c# - ASP.NET MVC4 Web API Controller 卡在一条路线上

标签 c# asp.net-mvc asp.net-mvc-4 asp.net-web-api asp.net-web-api-routing

我创建了一个新的 ASP.NET MVC4 Web Api 项目。除了默认的ValuesController ,我添加了另一个 Controller ,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 类,没有修改任何东西):

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";
        }
    }
}

最佳答案

小 Sprite 。感谢@Pete Klien 验证代码在我的机器外是否工作。这是我所做的。

  1. 在原始项目中遇到了 Controller 仅使用一种方法获取 Get 的问题。
  2. 使用我在问题中发布的代码创建了新的 Web Api 项目。相同的症状。
  3. 清理项目,重建所有,仍然没有骰子。
  4. 重启机器,清理,重建,再试一次,没有骰子。
  5. 在新解决方案中创建新的 Web Api 项目,成功!

关于c# - ASP.NET MVC4 Web API Controller 卡在一条路线上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16472558/

相关文章:

c# - 将 DLL 加载到单独的 AppDomain 中

c# - Windows 窗体/控件中 *.resx 文件的相关性是什么?

asp.net - HttpHandler 在 ASP.NET MVC3 Web 应用程序中不起作用

asp.net - 如何为 Release模式设置debug false

javascript - 使用 MVC 4 从 View 传递模型数据但不显示在操作中

c# - regasm/codebase 有效,但使用 regasm/codebase/regfile 生成的文件无效

c# 创建任意长度的通用参数字符串,例如,?,?

c# - Asp.NET MVC 模型绑定(bind) - 使用绑定(bind)参数属性为简单类型分配前缀

asp.net-mvc - 如何在没有页面构建器的情况下在 Kentico 12 中使用 BizForms

asp.net-mvc-3 - 在 MVC3 项目中使用 Apicontroller