Search code examples
c#asp.netangularjsasp.net-web-apiasp.net-web-api-routing

Multiple HttpPost action with the same parameter in Web API Controller


I'm developing a web application using ASP.NET Web Api and angularJs. I have a web api controller like this:

[ActionName("AddNewState")]
public object PostAddNewState(RegionInformation newStateParam)
{
    RegionOperations regionOperation = new RegionOperations(newStateParam.StateName);
    RegionInformation newStateInformation = regionOperation.NewStateInformation;
    var text = new
    {
        newStateInformation
    };
    return JsonConvert.SerializeObject(text);
}

[ActionName("AddNewCity")]
public object PostAddNewCity(RegionInformation newCityParam)
{
    var text = new
    {
        message="Hello"
    };
    return JsonConvert.SerializeObject(text);
}

and in client side I have these functions for sending POST request:

$scope.AddNewState = function () {
    $http({
        method: "POST",
        url: "api/RegionManagement/AddNewState",
        data: {
            StateName: $scope.state
        }
    }).then(function (response) {
        var obj = JSON.parse(response.data);
        $scope.States.push({ text: obj.newStateInformation.StateName, value: obj.newStateInformation.ID });
    });
};

$scope.AddNewCity = function () {
    $http({
        method: "POST",
        url: "api/RegionManagement/AddNewCity",
        data: {
            ParentID: $scope.RegionInptes.ParentID,
            CityName: $scope.city
        }
    }).then(function (response) {
        var obj = JSON.parse(response.data);
        alert(obj.message);
    });
};

When I execute $scope.AddNewCity or $scope.AddNewState I face with 500 Internal Server Error.if I comment AddNewCity action in web api controller then I can execute $scope.AddNewState successfully.

I searched for using multiple HTTPPost in a web api controller and try this solution: Multiple HttpPost method in Web API controller, but nothing happened and I still have that error.

UPDATE

This is my config file:

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

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "ControllerOnly",
        routeTemplate: "api/{controller}"
    );

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

Solution

  • The order in which you register your routes is important. register more specific routes first and the more general routes after.

    public static void Register(HttpConfiguration config) {
        // Web API configuration and services
    
        // Web API routes
        config.MapHttpAttributeRoutes();
    
        config.Routes.MapHttpRoute(
            name: "ControllerAndAction",
            routeTemplate: "api/{controller}/{action}/{id}"
            defaults: new { id = RouteParameter.Optional }
        );
    
        config.Routes.MapHttpRoute(
            name: "ControllerandId",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    
    }
    

    You should also update the controller to be a little more specific as to what it can handle.

    public class RegionManagementController : ApiController {
        [HttpPost]
        [ActionName("AddNewState")]
        public object PostAddNewState(RegionInformation newStateParam) { ... }
    
        [HttpPost]
        [ActionName("AddNewCity")]
        public object PostAddNewCity(RegionInformation newCityParam) { ... }
    }