我知道这个问题已经在许多领域被问过和回答,因为我已经搜索并看到了许多不同的答案和例子;但是,我无法应用其中任何一个并使其正常工作。
我正在尝试在 MVC 4 中组合一个 RPC 样式的 apicontroller。在 aipcontroller 中,我有两种方法正在尝试执行发送和接收 JSon:TestFunction1 和 TestFunction2。如果我注释掉其中一个,另一个可以正常工作。
当我尝试同时包含它们时,它会失败并出现以下(常见)错误:
Multiple actions were found that match the request:
TestFunction1 on type test.api.TestController
TestFunction2 on type test.api.TestController"
尽我所能,我正在遵循我找到的示例,并且我已经阅读了 SO QnAs,直到我失明并且我仍然无法弄清楚我做错了什么。
有人可以帮我解决并理解这一点吗?
编辑:附加说明 - 如果我注释掉 TestFunction2 并且不更改我的 javascript 操作以匹配,我仍然会收到对 TestFunction1 的调用。
我的控制器方法:
[HttpPost, ActionName("TestFunction1")]
public HttpResponseMessage TestFunction1(SomeModel somemodel)
{ SomeModel otherModel = new SomeModel() {
id = 7,
username = "other API-PostSomeModel Username",
email = "other API-PostSomeModel email",
password = "other API-PostSomeModel passowrd"
};
if (ModelState.IsValid) {
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, somemodel);
var serializer = new JavaScriptSerializer();
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = somemodel.id }));
response.Content = new StringContent(serializer.Serialize(otherModel));
return response;
}
else {
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
[HttpPost, ActionName("TestFunction2")]
public HttpResponseMessage TestFunction2(SomeModel somemodel)
{ SomeModel otherModel = new SomeModel() {
id = 7,
username = "other API-TestFunction Username",
email = "other API-TestFunction email",
password = "other API-TestFunction passowrd"
};
var serializer = new JavaScriptSerializer();
var resp = new HttpResponseMessage() {
Content = new StringContent(serializer.Serialize(otherModel))
};
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return resp;
}
该模型:
public class SomeModel
{ [Key]
public long id { get; set; }
public string username { get; set; }
public string password { get; set; }
public string email { get; set; }
public SomeModel() {}
public SomeModel(long _id, string usrname, string pswd, string _email) {
id = _id;
username = usrname;
password = pswd;
email = _email;
}
}
我的路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Route Catches the GET PUT DELETE typical REST based interactions (add more if needed)
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new {
id = RouteParameter.Optional,
httpMethod = new HttpMethodConstraint("GET", "PUT", "DELETE")
}
);
//This allows POSTs to the RPC Style methods http://api/controller/action
routes.MapHttpRoute(
name: "API RPC Style",
routeTemplate: "api/{controller}/{action}",
defaults: new {
httpMethod = new HttpMethodConstraint("POST"),
}
);
//Finally this allows POST to typical REST post address http://api/controller/
routes.MapHttpRoute(
name: "API Default 2",
routeTemplate: "api/{controller}/{action}",
defaults: new {
httpMethod = new HttpMethodConstraint("POST"),
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
我的JavaScript:
function jsTestFunction(id, username, password, email) {
alert("Verify data:" + "\n\r" + id + "\n\r" + username + "\n\r" + password + "\n\r" + email);
var obj = {
'id' : id,
'username' : username,
'password' : password,
'email' : email,
};
var postdata = JSON.stringify(obj);
$.ajax({
type: "POST",
url: "api/Test/TestFunction2/",
dataType: "json",
traditional: true,
data: postdata,
contentType: "application/json; charset=utf-8",
success: function (responsedata) {
var temp0 = responsedata['id'];
var temp1 = responsedata['username'];
var temp2 = responsedata['password'];
var temp3 = responsedata['email'];
if (!responsedata.error) {
alert("The process was successful: " + "\n\r" + temp0 + "\n\r" + temp1 + "\n\r" + temp2 + "\n\r" + temp3);
}
else {
alert("An error occured during the process: " + responsedata.msg);
$('#reservation-result').html("Error :" + responsedata.msg).fadeIn(2000);
}
},
error: function (error) {
alert("There was an error posting the data to the server: " + error.responseText);
}
});
}