Multiple actions were found that match the request: in web api C# ?


HI, I am trying to work with the Web API in C#, and when calling it using jquery I am getting the error "Multiple actions were found that match the request: in web API C#", so how to resolve this issue, here is my current code

public HttpResponseMessage Create(Student stu)
        {
            HttpResponseMessage result;
            try
            { 
                   //some code here
            } 
            
            catch (Exception e)
            {  
                result = Request.CreateResponse(HttpStatusCode.BadRequest, "Error");  
            }  
            return result;  

           
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage Edit(Student stu)
        {
            HttpResponseMessage result;
            try
            {
              //Some code here

                result = Request.CreateResponse(HttpStatusCode.Created, blog);
            }

            catch (Exception e)
            {
                result = Request.CreateResponse(HttpStatusCode.BadRequest, "Error");
            }
            return result;


        }

as you can there are two methods with same argument but different name and method type also, so what's the issue with the code, how to resolve this error?


Asked by:- neena
0
: 1542 At:- 12/5/2017 11:34:36 AM
C# MVC Web-api multiple actions were found error asp.net






2 Answers
profileImage Answered by:- vikas_jk

If you haven't read CRUD with Web api article(error explained here), please check this article https://qawithexperts.com/article/asp.net/create-edit-delete-using-aspnet-web-api-jquery-datatable-ser/76

You may get an error "Multiple actions were found that match the request" if you have two methods of same types, as In Web API (by default) methods are chosen based on a combination of HTTP method and route values.

What you have shown is a route for MVC controllers. I hope you realize that Web API controllers are an entirely different thing. They have their own routes defined in the ~/App_Start/WebApiConfig.cs

Solution

Either change your RuoteConfig.cs to make this work

config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = System.Web.Http.RouteParameter.Optional }
    );

OR add Route Attribute at the top of your method, something like this

        [System.Web.Http.Route("api/ControllerApi/Edit")] // to avoid  "Multiple actions were found that match the request Web API"
        public HttpResponseMessage Edit(Student Stu)
        {
          //Code here


        }
2
At:- 12/5/2017 4:11:42 PM Updated at:- 12/5/2017 4:20:26 PM
Second solution of Route Attribute works for me 0
By : neena - at :- 12/9/2017 2:15:53 PM


profileImage Answered by:- user_1889694638

Use attribute ActionName  on top of method

0
At:- 12/5/2017 12:19:39 PM Updated at:- 12/5/2017 12:20:12 PM
tried it, it didn't worked 0
By : neena - at :- 12/9/2017 2:15:30 PM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use