1

In my TestController I have the following:

 [HttpGet]
    public IEnumerable<String> Active()
    {
        var result = new List<string> { "active1", "active2" };

        return result;
    }

    [HttpGet]
    public String Active(int id)
    {
        var result = new List<string> { "active1", "active2" };

        return result[id];
    }

In RouteConfig the mapping is:

 routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        routes.MapHttpRoute(
            name: "ActionApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { action = "", id = RouteParameter.Optional });

In a browser the following request works:

api/test/active/1

But this returns a Internal Server Error:

api/test/active

What do you have to do to return a action that may or maynot have a parameter in a similar manner to the default Get?

Update 1 As Cuong Le suggested, changing the ordering of routes helped, the routes are now:

 routes.MapHttpRoute(
            name: "ActionApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

I had to remove action = "" from the ActionApi route otherwise the standard Get on the other controllers stopped working (i.e. api/values)

api/test/active is now resolving, but I now get a 500 Internal Server Error for /api/test is it possile to have both resolves, so api/test would return "all" and /test/active only return "some"?

3
  • What kind of Internal Server Error you get? Multiple actions were found? Commented Jul 30, 2012 at 16:40
  • Sorry I am not sure how to get a more detailed message, I am running just using debug in Visual Studio, is there a web.config setting or something to get a more verbose error? Commented Jul 30, 2012 at 17:17
  • I have not tried to debug bu I think you can see verbose error from debugging. You also can set up <httpErrors errorMode="Detailed"/> on web.config and use filter to see the error thrown out. As your questions, I have not clear answer so far :(. Commented Jul 31, 2012 at 11:14

2 Answers 2

0

It is probably getting confused since you have two methods named action. Try deleting or renaming one of them and see if that works.

Sign up to request clarification or add additional context in comments.

1 Comment

If I comment out Active(int id) in the controller I get a 400 Bad Request for the request to api/test/active. I would have thought two methods one with an one without parameters would be OK as the default example Get() and Get(int id) work.
0

One way to do it is to provide a default value for the parameter,

[HttpGet]
public String Active(int id = 0)
{
    var result = new List<string> { "active1", "active2" };

    if (id == 0) {
      return result;
    } else {
      return result[id];
    }
}

1 Comment

Darrel, the return type will be different, if id == 0 a Enumerable would be returned. What I am trying to understand is the routing when you add "actions" to the Url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.