0

In my MVC controller, I have two methods with the same action name but with different routing attributes so they respond differently to GET and POST requests (modeled by this link):

[HttpGet]
public string Test()
{
    return "get";
}

[ActionName("Test")]
[HttpPost]
public string Test_Post()
{
    return "post";
}

and the corresponding URI is just Sample/Test. I've also enabled attribute routing with routes.MapMvcAttributeRoutes() in my RouteConfig.cs.

However, if I send a GET or POST request to this URI, it always calls Test(), never Test_Post(). Why are the requests routed to the same method and ignoring the route attributes?

EDIT: To clarify, I send the request with an AJAX call:

$.post("https://{baseURL}/Sample/Test", function(result){
        console.log(result);
    }});

which always prints "get" via Test() instead of "post" via Test_Post().

Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
Booley
  • 819
  • 1
  • 9
  • 25

1 Answers1

0

Solved! Turns out I was using the System.Web.Http namespace in my MVC controller, but the ActionName, AcceptVerbs, etc. attributes exist in both the Http and Mvc namespaces. I explicitly referenced the System.Web.Mvc namespace and the attributes work fine now.

Booley
  • 819
  • 1
  • 9
  • 25