简体   繁体   中英

ASP.NET MVC4 Web API Controller Stuck on One Route

I created a new ASP.NET MVC4 Web Api Project. In addition to the default ValuesController , I added another controller, ScenarioController . It has the exact same methods as 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) . Why?

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

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 (yes, it's in the Controllers folder)

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

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. Here's what I did.

  1. Experienced problem of Controller only using 1 method for Get in original project.
  2. Created new Web Api project, with code that I posted in the 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!

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. It was localhost:57846. I just made it localhost:57847 and all is back to normal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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