Asp.net web api WEB API。找到多个与请求匹配的操作
我需要帮助 我有两个控制器:Asp.net web api WEB API。找到多个与请求匹配的操作,asp.net-web-api,Asp.net Web Api,我需要帮助 我有两个控制器: // POST (Single SMS) [ActionName("AddSMS")] public HttpResponseMessage Post(MySMS singleSMS) { try { SMS_Repository.Add(singleSMS); return Request.CreateResponse<MySMS>(HttpStatusCode.Created, singleSMS);
// POST (Single SMS)
[ActionName("AddSMS")]
public HttpResponseMessage Post(MySMS singleSMS)
{
try
{
SMS_Repository.Add(singleSMS);
return Request.CreateResponse<MySMS>(HttpStatusCode.Created, singleSMS);
}
catch (Exception)
{
return Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Error");
}
}
// POST (Collection of SMSes)
[ActionName("AddSMSCollection")]
public HttpResponseMessage Post(List<MySMS> smses)
{
try
{
SMS_Repository.Add(smses);
return Request.CreateResponse<List<MySMS>>(HttpStatusCode.Created, smses);
}
catch (Exception)
{
return Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "errorus");
}
}
现在。如果我发送这样的请求:
localhost:25856/api/sms/AddSMSCollection
它起作用了
是否可以优化路由,以便我可以使用localhost:25856/api/sms,并且没有发现与请求错误匹配的多个操作
对不起,我的英语不好。您可以定义在这种情况下应执行的操作:
defaults: new { id = RouteParameter.Optional, action = "AddSMS" }
但是,由于只有以下url
localhost:25856/api/sms
,并且不包括操作名称,我希望您认识到路由引擎无法消除要执行的操作的歧义。路由引擎可以使用HTTP动词,但在您的情况下,两个操作都是POST。Darin,您的示例不起作用。当我使用localhost:25856/api/sms时,如果请求主体是MySMS singleSMS,Fiddler将返回417,但如果我发送了collection,则返回417
defaults: new { id = RouteParameter.Optional, action = "AddSMS" }