0

I'm currently working at a .NET 4.5 MVC 4 Web Application. I have got the following Routes:

            routes.MapRoute(
            name: "Default",
            url: "api/",
            defaults: new { controller = "Response", action = "ReturnAllStations" }
        );


        routes.MapRoute(
            name: "ID",
            url: "api/{id}",
            defaults: new { controller = "Response", action = "ReturnStuffA", id = UrlParameter.Optional }
        );

Now when I enter the URL http://localhost:55302/api/ it all works fine. But when I enter an URL like this: http://localhost:55302/api/SampleId1234 I get the following error "No type was found that matches the controller named 'Sample1234'."

Why does it try to get a controller named Sample1234 and not the defautlt one and use sample1234 as parameter?

SwissPrime
  • 443
  • 1
  • 6
  • 10

1 Answers1

1

Your default route should come last. Route config will look for the configuration from top to bottom and when it finds a match immediately returns invokes that action.

In your case always invoke the first configuration because it matches the api/ configuration.

Saanch
  • 1,814
  • 1
  • 24
  • 38
  • I changed the order but now I get the same error with the api/{id} and I get the response that the resource can't be found with only api/ – SwissPrime Nov 27 '13 at 13:12
  • with this`http://localhost:55302/api/SampleId1234` what are you trying to invoke? is it `Response` controllers `ReturnStuffA` action with SampleId1234 as `id` parameter? If thats the case you have to create ReturnStuffA action that accepts a string parameter named id. – Saanch Nov 27 '13 at 14:09