请求匹配多个端点SMS API控制器



我正在努力将Nexmo API实现到我的代码中,并且遇到了控制器的一些问题。我得到错误

AmbiguousMatchException:请求匹配了多个端点。匹配:gardenplanning.Controllers.SMSController.Send (gardenplanning)gardenplanning.Controllers.SMSController.Send (gardenplanning)Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector。ReportAmbiguity (CandidateState [] CandidateState)

我有一种感觉,它与2 "发送"方法,但我的想法,如何修改其中一个来修复路由。代码集成到我的项目如下。我注释掉了Index方法因为在我的HomeController中它已经有映射到Index的方法。如能提供任何帮助,我将不胜感激,谢谢。

输入代码

namespace gardenplanning.Controllers {
public class SMSController : Controller
{
/*
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
*/
//Send Action Method
[System.Web.Mvc.HttpGet]
public ActionResult Send()
{
return View();
}
//Action method containing "to" and "text" params
[System.Web.Mvc.HttpPost]
public ActionResult Send(string to, string text)
{
var results = SMS.Send(new SMS.SMSRequest
{
from = Configuration.Instance.Settings["appsettings:NEXMO_FROM_NUMBER"],
to = to,
text = text
});
return View();
}
}
}

Nexmo API指南如下

public ActionResult Index()
{
return View();
}
[System.Web.Mvc.HttpGet]
public ActionResult Send()
{
return View();
}

[System.Web.Mvc.HttpPost]
public ActionResult Send(string to, string text)
{
var results = SMS.Send(new SMS.SMSRequest
{
from = Configuration.Instance.Settings["appsettings:NEXMO_FROM_NUMBER"],
to = to,
text = text
});
return View("Index");
}

AmbiguousMatchException:请求匹配多个端点。匹配:gardenplanning. controllers . smscontroller . send (gardenplanning)ReportAmbiguity (CandidateState [] CandidateState)

如异常消息所示,请求匹配了两个或多个端点,这导致了问题。

要解决不明确的操作,您可以尝试将不同的HTTP动词模板应用于这两个"Send"action方法,它将匹配限制为仅具有特定HTTP方法的请求。在ASP中。. NET Core应用程序,你可以使用[Microsoft.AspNetCore.Mvc.HttpGet]代替[System.Web.Mvc.HttpGet]

[HttpGet]
public ActionResult Send()
{
return View();
}
//Action method containing "to" and "text" params
[HttpPost]
public ActionResult Send(string to, string text)
{
var results = SMS.Send(new SMS.SMSRequest
{
from = Configuration.Instance.Settings["appsettings:NEXMO_FROM_NUMBER"],
to = to,
text = text
});
return View();
}

此外,如果你想根据查询字符串有条件地启用或禁用给定请求的操作,你可以尝试实现自定义ActionMethodSelectorAttribute。欲了解更多信息,请查看此博客:

https://devblogs.microsoft.com/premier-developer/defining-asp-net-core-controller-action-constraint-to-match-the-correct-action/

最新更新