1

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}"
        );
}
Community
  • 1
  • 1
  • 1
    Show your web api config. most probably the routeTemplate is `{controller}/{id}` if so then that is why you get the error as the framework is unable to determine which action you are trying to call. update template to include action `{controller}/{action}/{id}` and see if that works – Nkosi Dec 15 '16 at 11:19
  • @ Nkosi i add my config file in post update – Abolfazl Davoodi Shandiz Dec 15 '16 at 21:16

2 Answers2

0

May be this is because of the number of parameters in the request url

[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)
{

    RegionOperations regionOperation = new RegionOperations(newCityParam.ParentID, newCityParam.CityName);
    RegionInformation newStateInformation = regionOperation.NewStateInformation;
    var text = new
    {
        newStateInformation
    };
    return JsonConvert.SerializeObject(text);
}
Mr_Perfect
  • 8,254
  • 11
  • 35
  • 62
0

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) { ... }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472