0

我构建了一个简单的联系表单,在我的测试“mvc1application”中本地工作得很好。当我将确切的代码传输到我的一个网站时,我会遇到问题并且真的很难过。

场景是当用户访问站点时,它会检查他们是否已经通过身份验证(通过 SSO 系统)。如果是这样,则设置了 cookie 并且他们已登录。如果您尝试在未登录的情况下访问该站点,您当前会被重定向到“AccessDenied.cshtml”视图。这完全没有问题。

从那以后,我用一个表单(简单的联系表单)替换了该视图,用户可以填写该表单以向网站管理员发送电子邮件。我让表单正确显示,在 POST 上它得到 200,通过 Chrome 开发工具,我看到表单内容已发布。没有发生的是我认为我的 EmailViewModel.cs 没有被调用并处理表单数据。所以从来没有发送过电子邮件。

我真的很难过,这里是一些示例代码:

(我的 AccountController 的一些部分被剪掉了......)

[AllowAnonymous]
public ActionResult LogOn(string strEmail, string token)
{
    DepartmentUser departmentuser = db.DepartmentUsers.SingleOrDefault(r => r.UserEmail == strEmail && r.Token == token);
    if (departmentuser != null)
    {
        //verify that the token date  + 1 day >= current datetime
        if (departmentuser.TokenDate.Value.AddDays(1) >= DateTime.Now)
        {
            //if yes, then update to set token = null and datetime = null
            departmentuser.Token = null;
            departmentuser.TokenDate = null;
            db.SaveChanges();

            string userData = departmentuser.DepartmentUserID.ToString() + ":" + departmentuser.DepartmentID.ToString() + ":" + departmentuser.UserName;

            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, departmentuser.UserEmail,
                DateTime.Now, DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
                false, userData);
            string hashedTicket = FormsAuthentication.Encrypt(ticket);

            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashedTicket);
            Response.AppendCookie(cookie);
            return Redirect("~/Home");
        }
        else
        {
            return View("RequestForm");

        }
    }
    else
    {
        return View("RequestForm");
    }
}

public ActionResult RequestForm(EmailViewModel emailVM)
{
    if (!ModelState.IsValid)
    {
        return View(emailVM);
    }

    var email = new EmailViewModel
    {

        Name = emailVM.Name,
        EmailAddress = emailVM.EmailAddress,
        InternalTeamName = emailVM.InternalTeamName,
        InternalTeamLeadName = emailVM.InternalTeamLeadName,
        InternalDistEmail = emailVM.InternalDistEmail,
        AverageNumCommsWeekly = emailVM.AverageNumCommsWeekly,
        AverageNumPeopleSentWeekly = emailVM.AverageNumPeopleSentWeekly
    };

    string body = "Name: " + email.Name + "\n"
                + "Email: " + email.EmailAddress + "\n"
                + "Internal Team: " + email.InternalTeamName + "\n"
                + "Internal Team Lead: " + email.InternalTeamLeadName + "\n"
                + "Internal Team Lead Email: " + email.InternalTeamLeadEmail + "\n"
                + "Internal Distribution Email: " + email.InternalDistEmail + "\n"
                + "Average # of Communications Per Week: " + email.AverageNumCommsWeekly + "\n"
                + "Average # of People Emailed Per Week: " + email.AverageNumPeopleSentWeekly;

    MailMessage mail = new MailMessage();

    mail.From = new MailAddress(email.EmailAddress);
    // for production put email in web.config for easy change
    mail.To.Add("user@email.me");
    mail.Subject = "Access Request";
    mail.Body = body;
    mail.IsBodyHtml = true;

    // smtp is local directory in web.config for testing ATM...
    SmtpClient smtp = new SmtpClient();

    smtp.Send(mail);

    mail.Dispose();
    return View("RequestForm");

}

还有我的 EmailViewModel.cs...

namespace MVC.Models
{
    public class EmailViewModel
    {
        [Required]
        public string Name { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        public string EmailAddress { get; set; }

        [Required]
        public string InternalTeamName { get; set; }

        [Required]
        public string InternalTeamLeadName { get; set; }

        [Required]
        public string InternalTeamLeadEmail { get; set; }

        [Required]
        public string InternalDistEmail { get; set; }

        [Required]
        public string AverageNumCommsWeekly { get; set; }

        [Required]
        public string AverageNumPeopleSentWeekly { get; set; }


    }
}

最后是我的 Requestform.cshtml 视图...

@model CorpNewsMgr.Models.EmailViewModel
@{
    ViewBag.Title = "Request Access";
    Layout = "~/Views/Shared/_WideLayoutDenied.cshtml";
}
<p>
    Currently you do not have the required credentials to access the site.
    <br />
    To request more information, or to request permission to access this system, please
    contact:
</p>
<br />
<br />
<div>
    <h3>
        Please fill this form out to request access to the tool.</h3>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Request Access</legend>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.Name)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.Name)
                @Html.ValidationMessageFor(Model => Model.Name)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.EmailAddress)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.EmailAddress)
                @Html.ValidationMessageFor(Model => Model.EmailAddress)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamName)
                @Html.ValidationMessageFor(Model => Model.InternalTeamName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamLeadName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamLeadName)
                @Html.ValidationMessageFor(Model => Model.InternalTeamLeadName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalTeamLeadEmail)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalTeamLeadEmail)
                @Html.ValidationMessageFor(Model => Model.InternalTeamLeadEmail)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.InternalDistEmail)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.InternalDistEmail)
                @Html.ValidationMessageFor(Model => Model.InternalDistEmail)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.AverageNumCommsWeekly)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.AverageNumCommsWeekly)
                @Html.ValidationMessageFor(Model => Model.AverageNumCommsWeekly)
            </div>
            <div class="editor-label">
                @Html.LabelFor(Model => Model.AverageNumPeopleSentWeekly)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(Model => Model.AverageNumPeopleSentWeekly)
                @Html.ValidationMessageFor(Model => Model.AverageNumPeopleSentWeekly)
            </div>
            <p>
                <input type="submit" value="Send" />
            </p>
        </fieldset>

    }
</div>
<br />
<p>
    Thank you,<br />

</p>

因此,当页面加载时,表单加载正常(耶)。我可以填写并验证它(耶)。在提交时我得到一个 200 并且 POST 看起来很好(我可以看到 POST 中的字段)但没有任何反应。页面重新加载为空表单,我的取件文件夹或任何内容中都看不到任何电子邮件。

同样,在我的本地开发测试站点上,我可以让它工作并正常运行。在这里我不能。我认为问题在于 AccountController 的顶部以及它是如何拉视图的,但我很难(并且很累)试图弄清楚这一点。

哦,这是我从标准 WebForms 构建 MVC 表单的第一次大尝试……

有什么想法吗?

谢谢!

4

1 回答 1

1

我正要评论,但它运行得太久了。

您需要发布发布到的操作,Requestform.cshtml这里也不需要您的帐户控制器代码。我的猜测是您的托管环境是共享托管,并且有一些东西阻止您的电子邮件代码运行。

您可能想要做的第一件事是,如果您的电子邮件代码在 atry catch中,请暂时评论我们的 the,try catch以便它会抛出错误。这至少应该可以帮助您了解正在发生的事情。

接下来要做的是尝试Elmah为 mvc3 添加,您可以通过 Visual Studios 中的 Nuget 添加它。确保为 MVC3 使用 Elmah,因为它会为您完成大部分设置。这将让您看到是否有任何错误被抛出。

否则,如果没有看到您的实际电子邮件代码,我将无法提供更多帮助。

于 2012-09-08T13:06:06.957 回答