Published on: 17 May, 2016

Multiple actions were found that match the request in MVC Web API C# ASP.Net

Posted by andy | 1519 views 0 likes 0 favourites 1 comments
Tumblr Digg Google Plus

If you are completely new to MVC Web API and encounter a problem with an error message saying Multiple actions were found that match the request. It means you need to modify your Web API register class to provide additional action request in the code.

If you open the WebApiConfig.cs under the App_Data folder under your root project, you will have the following code by default.

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

What you need to do is to modify the routeTemplate and add extra action parameter into the code. The changes would be like below.

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
Comments
Lalit
Commented on: 30 Apr, 2017 05:28:40 PM
Thanks bro
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.
Related Articles
Published on: 28 October, 2016

In this tutorial you will learn how easily you can implement a simple checkout express using C# MVC Web API. We will create a really simple shopping cart where customers can add and delete their cart items before proceed to payment.