Web API控制器中具有相同参数的多个HttpPost操作

时间:2016-12-15 10:13:41

标签: c# asp.net angularjs asp.net-web-api asp.net-web-api-routing

我正在使用ASP.NET Web Api和angularJs开发Web应用程序。 我有一个像这样的web api控制器:

[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);
}

在客户端我有这些函数用于发送 POST 请求:

$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);
    });
};

当我执行 $ scope.AddNewCity $ scope.AddNewState 时,我面临 500内部服务器错误。如果我发表评论在web api控制器中执行AddNewCity 操作,然后我就可以成功执行 $ scope.AddNewState

我搜索了在web api控制器中使用多个HTTPPost并尝试此解决方案:Multiple HttpPost method in Web API controller,但没有任何反应,我仍然有错误。

更新

这是我的配置文件:

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}"
        );
}

2 个答案:

答案 0 :(得分:0)

可能是因为请求网址中的参数数量

[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);
}

答案 1 :(得分:0)

您注册路线的顺序非常重要。首先注册更具体的路线,然后注册更一般的路线。

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 }
    );

}

您还应该更新控制器,使其更具体地说明它可以处理的内容。

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

    [HttpPost]
    [ActionName("AddNewCity")]
    public object PostAddNewCity(RegionInformation newCityParam) { ... }
}