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

C# Web API控制器中具有相同参数的多个HttpPost操作,c#,asp.net,angularjs,asp.net-web-api,asp.net-web-api-routing,C#,Asp.net,Angularjs,Asp.net Web Api,Asp.net Web Api Routing,我正在使用ASP.NETWebAPI和angularJs开发一个web应用程序。 我有这样一个web api控制器: [ActionName("AddNewState")] public object PostAddNewState(RegionInformation newStateParam) { RegionOperations regionOperation = new RegionOperations(newStateParam.StateName); RegionIn

我正在使用ASP.NETWebAPI和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.AddNewCity或$scope.AddNewState时,我会遇到500个内部服务器错误。如果我在web api控制器中注释AddNewCity操作,则我可以成功执行$scope.AddNewState

我搜索了在web api控制器中使用多个HTTPPost,并尝试了以下解决方案:,但什么都没有发生,我仍然有那个错误

更新

这是我的配置文件:

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

这可能是因为请求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);
}

这可能是因为请求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);
}

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

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) { ... }
}

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

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) { ... }
}

显示您的web api配置。routeTemplate很可能是
{controller}/{id}
,如果是,那么这就是为什么会出现错误,因为框架无法确定您尝试调用的操作。更新模板,使其包含action
{controller}/{action}/{id}
,并查看是否有效@Nkosi在更新后显示web api配置时添加我的配置文件。routeTemplate很可能是
{controller}/{id}
,如果是,那么这就是为什么会出现错误,因为框架无法确定您尝试调用的操作。更新模板以包含action
{controller}/{action}/{id}
并查看它是否有效@Nkosi我将我的配置文件添加到updateCharan Cherry中我认为这是因为与您相同的参数,但它应该像我的代码一样,在每个动作方法中使用相同的参数Charan Cherry我想这是因为和你一样的参数,但它应该像我的代码一样,在每个操作方法中使用相同的参数谢谢…它起作用了…注册路由的顺序就是解决方案谢谢…它起作用了…注册路由的顺序就是解决方案