我的控制器中有几个post方法。每当我将数据发布到此控制器时,只会调用第一个POST方法。我的要求是调用第二种方法,因为两种方法的参数都不同。这是路由配置:
config.Routes.MapHttpRoute(
name: "AddUser",
routeTemplate: "api/users/adduser",
defaults: new { controller = "users" }
);
config.Routes.MapHttpRoute(
name: "ChangeUser",
routeTemplate: "api/users/changeuser",
defaults: new { controller = "users" }
);
这是我的控制器代码:
[AllowAnonymous]
[ActionName("adduser")]
public string PostDetails(JObject userData)//Always this method is called.
{
//My code here
}
[AllowAnonymous]
[ActionName("changeuser")]
public string ChangeUser(int userId)
{
//My code here
}
从视图中调用:
Ext.Ajax.request( { url: 'localhost/myapp/api/users/changeuser'
, mode: 'POST'
, params: { userID: 1 }
}
);
答案 0 :(得分:1)
在路线配置中添加约束将解决您的问题。请尝试以下配置..
config.Routes.MapHttpRoute(
name: "AddUser",
routeTemplate: "api/{controller}/{action}",
defaults: new { },
constraints: new { controller = "users", action = "adduser" }
);
config.Routes.MapHttpRoute(
name: "ChangeUser",
routeTemplate: "api/{controller}/{action}",
defaults: new { },
constraints: new { controller = "users", action = "changeuser" }
);
C#部分:
[AllowAnonymous]
[ActionName("adduser")]
[AcceptVerbs("Post")]
public string PostDetails(JObject userData)//Always this method is called.
{
//My code here
}
[AllowAnonymous]
[ActionName("changeuser")]
[AcceptVerbs("Post")]
public string ChangeUser(int userId)
{
//My code here
}
答案 1 :(得分:0)
尝试:
[HttpPost, ActionName("Name")]
而不是:
[ActionName("Name")]
我不是专家,但可能会这样。