AmbiguousActionException:多个动作匹配。以下操作匹配路由数据并满足所有约束:

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

我知道有更多关于此的问题,但它们似乎对我不起作用。

我遵循了以下教程:https://www.youtube.com/watch?v=hlZexx2gbNs

我想在 MVC 中制作电子邮件发送表单 ASP.NET。 除了最后一步外,一切似乎都有效。我必须更改 RouteConfig,但我没有路由配置。

我想我需要更改它才能正常工作,但我不知道在哪里更改它。有谁知道如何解决这个问题?

如果在另一个问题上有好的解决方案,请告诉我。

这是我用于视图的代码:

@model smoothboard.Models.gmail

@{
ViewData["Title"] = "Index";
}

<h2>Index</h2>

<h4>Email</h4>
<hr />

<div>
    @using (Html.BeginForm("Index", "EmailSetup", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
    <table>
        <tr>
            <td>
                Naar:
            </td>
            <td>
                @Html.TextBoxFor(gmail=>gmail.To)
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Verzend">
            </td>
        </tr>
    </table>
    }
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

这是我用来发送邮件的代码:

public class EmailSetupController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
    string from = new string("hans31833@gmail.com");
    string subject = new string("Geregistreerd");
    string body = new string("Je hebt je ingeschreven voor de nieuwsbrief!");
    public IActionResult Index(smoothboard.Models.gmail gmail, string from)
    {

        MailMessage mm = new MailMessage(from, gmail.To);
        mm.Subject = subject;
        mm.Body = body;
        mm.IsBodyHtml = false;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;

        NetworkCredential nc = new NetworkCredential("hans31833@gmail.com", "hans1234hans");
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = nc;
        smtp.Send(mm);
        ViewBag.MailMessage = "Mail is verzonden";
        return View();
    }
}

这是我用于我的模型的:

namespace smoothboard.Models
{
public class gmail
{
    public string To { get; set; }
}
}

异常消息提到的问题很简单:您有 2 个具有相同名称和相同 HTTP 方法的操作方法(默认 HTTP 方法是 GET,隐式使用 [HttpGet]):

// first action with GET method
public IActionResult Index() { ... }

// ambiguous matching here because this also uses GET method
public IActionResult Index(smoothboard.Models.gmail gmail, string from) { ... }

因为你有 BeginForm 使用 FormMethod.Post 的助手:

@using (Html.BeginForm("Index", "EmailSetup", FormMethod.Post, new { enctype = "multipart/form-data" }))

那么你必须使用 [HttpPost] 属性用参数修饰第二个 Index 动作以区分其 HTTP 方法:

[HttpPost]
public IActionResult Index(smoothboard.Models.gmail gmail, string from)
{
    MailMessage mm = new MailMessage(from, gmail.To);
    mm.Subject = subject;
    mm.Body = body;
    mm.IsBodyHtml = false;

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.EnableSsl = true;

    NetworkCredential nc = new NetworkCredential("hans31833@gmail.com", "hans1234hans");
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = nc;
    smtp.Send(mm);
    ViewBag.MailMessage = "Mail is verzonden";
    return View();
}