简体   繁体   English

ASP.NET MVC4 Web API控制器被困在一条路线上

[英]ASP.NET MVC4 Web API Controller Stuck on One Route

I created a new ASP.NET MVC4 Web Api Project. 我创建了一个新的ASP.NET MVC4 Web Api项目。 In addition to the default ValuesController , I added another controller, ScenarioController . 除了默认的ValuesController ,我还添加了另一个控制器ScenarioController It has the exact same methods as ValuesController . 它具有与ValuesController完全相同的方法。 But for some reason, it behaves differently. 但由于某种原因,它表现不同。

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

Using breakpoints, I know that /api/scenario/1 actually gets sent to the public IEnumerable<string> Get() , not the expected public string Get(int id) . 使用断点,我知道/api/scenario/1实际上被发送到public IEnumerable<string> Get() ,而不是预期的public string Get(int id) Why? 为什么?

For reference, here are the relevant files (these are pristine default mvc4-webapi classes, haven't modified anything): 作为参考,这里是相关文件(这些是原始的默认mvc4-webapi类,没有修改任何东西):

Global.asax.cs 的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 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 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 (yes, it's in the Controllers folder) 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";
        }
    }
}

I tried your code just now and got the expected result: 我刚刚尝试了你的代码并获得了预期的结果:

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

(By the way, there is no requirement that it be in the Controllers folder. HttpConfiguration.Routes.MapHttpRoute simply finds all your classes that inherit from ApiController .) (顺便说一下,没有要求它在Controllers文件夹中HttpConfiguration.Routes.MapHttpRoute只是找到从ApiController继承的所有类。)

I am not being sarcastic when I suggest that you Rebuild All and try again. 当我建议你重建所有并再试一次时,我不是在讽刺。

Gremlins . 小鬼 Thanks to @Pete Klien for verifying that the code does work outside my machine. 感谢@Pete Klien验证代码在我的机器外部是否正常工作。 Here's what I did. 这就是我做的。

  1. Experienced problem of Controller only using 1 method for Get in original project. 控制器的经验问题仅在原始项目中使用1方法。
  2. Created new Web Api project, with code that I posted in the question. 创建了新的Web Api项目,其中包含我在问题中发布的代码。 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! 在新解决方案中创建新的Web Api项目, 成功!

I was having this issue and could not get anything to work. 我遇到了这个问题,无法正常工作。 Finally I changed the port on the IIS Express Project Url setting and all is back to normal. 最后,我更改了IIS Express Project Url设置上的端口,一切都恢复正常。 It was localhost:57846. 这是localhost:57846。 I just made it localhost:57847 and all is back to normal. 我刚做了localhost:57847,一切恢复正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM