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

1 Answer 1

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.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.