【问题标题】:Contact form ASP.NET C#联系表格 ASP.NET C#
【发布时间】:2021-02-16 12:28:16
【问题描述】:

我正在尝试在我的网页中创建“联系我们”表单,我尝试了一些方法,我认为它应该可以工作,但我仍然收到错误。

我试过这个:

HomeController.cs

   public class HomeController : Controller
    {
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Contact(ContactModel model, string email, string subject, string htmlMessage)
        {
            using (MailMessage mailMessage = new MailMessage())
            {

                mailMessage.From = new MailAddress("");
                mailMessage.Subject = subject;
                mailMessage.Body = htmlMessage;
                mailMessage.IsBodyHtml = true;
                mailMessage.To.Add(new MailAddress(email));
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp-mail.outlook.com";
                smtp.EnableSsl = true;
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                NetworkCred.UserName = "myemail@hotmail.com";
                NetworkCred.Password = "myEmailPassword";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                await smtp.SendMailAsync(mailMessage);
            }
            return View(model);
        }

        public ActionResult SuccessMessage()
        {
            return View();
        }
    }

ContactModel.cs

public class ContactModel
{
    public string Email { get; set; }
    public string Name { get; set; }
    public string Message { get; set; }
}

Contact.csshtml

@model ContactForSpacess.Models.ContactModel


<h2>ContactMail</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Contact</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

我收到以下错误: 应用程序中的服务器错误。找不到资源。 说明:HTTP 404。

感谢我能得到的所有想法和帮助!

谢谢。

【问题讨论】:

  • 试试:@using (Html.BeginForm("Contact", "Home", FormMethod.Post))

标签: c# asp.net email server-error


【解决方案1】:

您只定义了接收表单提交的操作 - HttpPostAttribute 意味着 GET 请求无法访问该操作。假设您希望显示的表单是您网站上的一个页面,您需要定义一个 GET 操作:

public ActionResult Contact()
{
    var model = new ContactModel();

    return View(model);
}

如果你愿意,你可以用[HttpGet] 来装饰动作,但这不是必须的。

如果需要,您还可以更新您的 Razor 助手以明确指定表单方法为 POST 并设置操作属性:

@using (Html.BeginForm("Contact", "Home", FormMethod.Post))
{
   @* ... your code *@
}

正如@asawyer 所述,HtmlHelper.BeginForm 扩展默认发出一个带有 method="post" 的表单:https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.html.formextensions.beginform?view=aspnet-mvc-5.2#System_Web_Mvc_Html_FormExtensions_BeginForm_System_Web_Mvc_HtmlHelper_

【讨论】:

  • 对,如果存在的话,我评论的更改就不需要了。
  • @asawyer 不,默认方法是 GET,在 &lt;form&gt; 元素 (w3.org/TR/html52/sec-forms.html#element-attrdef-form-method) 上,因此也需要这样做。如果您愿意将其添加到答案中,我会将其更改为社区 wiki。
  • @asawyer 我在答案中添加了这一点,但请随时详细说明。
  • 当不带参数调用时 .BeginForm - Writes an opening &lt;form&gt; tag to the response. The form uses the POST method, and the request is processed by the action method for the view. 由于问题中没有 Get Home/Contact,因此它的行为不正确是有道理的。添加一个后,根据您的响应,它应该按预期运行。但是您也可以只添加动作/控制器参数来指导帖子。 @JanAdams - 在浏览器调试器中使用 Fiddler 或网络选项卡来帮助诊断此类问题。它会准确地显示表单的发布位置。
  • 啊。 Razor 助手默认发出一个带有 method="post" 的表单?不知道我是否知道这一点——我很少使用空的 BeginForm。更新了答案。
【解决方案2】:

应该是这样的。

 [HttpGet]
    public async Task<IActionResult> Contact()
    {
        return View();
    }

    [HttpPost]
    [Route("postcontact")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> ContactMakePost([FromBody] Contact model)
    {
        return View();
    }

你的视图应该是这样的

@using (Html.BeginForm("postcontact", "Home", FormMethod.Post)){ }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    相关资源
    最近更新 更多