1

In my web api, I have created this controller:

public class DistributionGroupController : ApiController
{
    [HttpGet]
    public ServiceResult Index(string id)
    {
        if (id == null)
            return null;
        else
            return new ServiceResult();
    }
}

In addition, this is my route config. I am specifying my default action for my distribution groups route to be "Index":

    routes.MapHttpRoute(
        "Api action",
        "Api/{controller}/{action}"
    );
    routes.MapHttpRoute(
        "Api get",
        "Api/{controller}"
    );
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    /*This is the route in question*/
    routes.MapHttpRoute(
        "DistGroupRoute",
        "api/distributiongroup/{id}/{action}",
        new { controller = "DistributionGroup", action = "Index" }
    );

And in my view, I am using this script to (try to) hit my controller:

$.ajax({
    url: "api/distributiongroup/4567bn57n5754",
    cache: false,
    success: function (response) {
        alert('success');
    }
});

But my ajax call recieves a 404 Not Found error. However, if I append index to my url from my ajax call, my controller is hit. So, in essence, this does not work:

        api/distributiongroup/4567bn57n5754

But this does work:

        api/distributiongroup/4567bn57n5754/index

It's my understanding that my default action should get hit if I don't specify my action in my url. What might I be missing here? And, more importantly, how can I make my Index controller get hit when I use a url such as this:

        api/distributiongroup/4567bn57n5754

(without specifying the Index action?

Matt Spinks
  • 6,380
  • 3
  • 28
  • 47

2 Answers2

0
routes.MapHttpRoute(
    "DistGroupRoute",
    "api/distributiongroup/{id}",
    new { controller = "DistributionGroup", action = "Index" }
);

instead of:

routes.MapHttpRoute(
    "DistGroupRoute",
    "api/distributiongroup/{id}/{action}",
    new { controller = "DistributionGroup", action = "Index" }
);

There is no need to add the {action} in the route template because you already added it in the defaults object.

Basically, you said: Whenever there is an URL that matches this route template (api/distributiongroup/{id}/{action}) trigger the Index action in the DistributionGroup controller and pass the id parameter.

Anis Alibegić
  • 2,941
  • 3
  • 13
  • 28
0

This happened because of the order I was specifying my routes in my route config. I was specifying them in this order:

        routes.MapHttpRoute(
            "Api action",
            "Api/{controller}/{action}"
        );
        routes.MapHttpRoute(
            "Api get",
            "Api/{controller}"
        );
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        routes.MapHttpRoute(
            "DistGroupRoute",
            "api/distributiongroup/{id}/{action}",
            new { controller = "DistributionGroup", action = "Index" }
        );

Is seems that this route (my default route):

{controller}/{action}/{id}

was overriding my distributiongroups route when I did not specify index in my url. I'm still not entirely sure why this happened. But re-ordering my route configs fixed it. I just needed to put my distributiongroup route before my default route:

        routes.MapHttpRoute(
            "DistGroupRoute",
            "api/distributiongroup/{id}/{action}",
            new { controller = "DistributionGroup", action = "Index" }
        );
        routes.MapHttpRoute(
            "Api action",
            "Api/{controller}/{action}"
        );
        routes.MapHttpRoute(
            "Api get",
            "Api/{controller}"
        );
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
Matt Spinks
  • 6,380
  • 3
  • 28
  • 47
  • 1
    See [Why map special routes first before common routes in asp.net mvc?](https://stackoverflow.com/questions/35661062/why-map-special-routes-first-before-common-routes-in-asp-net-mvc/35674633#35674633) – NightOwl888 Jan 24 '18 at 19:55
  • That makes sense. Thanks. – Matt Spinks Jan 24 '18 at 19:56