我构建了一个简单的联系表单,在我的测试'mvc1application'中可以很好地工作。当我将确切的代码传输到我的网站之一时,我会遇到问题并且真的很难过。
场景是指用户点击它检查的站点以查看它们是否已经过身份验证(通过SSO系统)。如果是这样,则设置cookie并登录。如果您尝试在未登录的情况下访问该站点,则您当前将被重定向到“AccessDenied.cshtml”视图。这完全没问题。
我已经用一个表单(简单的联系表单)替换了该View,用户可以填写该表单以通过电子邮件发送给Web主页。我得到了正确显示的表单,在POST上它获得了200个并通过Chrome开发工具我看到表单内容是POSTED。没有发生的是我不认为我的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>
所以当页面加载时,表单加载正常(yay)。我可以填写并验证它(yay)。在提交我得到200和POST看起来很好(我可以看到POST中的字段)但没有任何反应。页面重新加载为空表单,我在我的提取文件夹或任何内容中都没有看到任何电子邮件。
同样,在我的本地开发测试网站上,我可以让它工作并通过ok路由。在这里,我不能。我认为问题在于AccountController的顶部以及它如何拉动视图,但我很难(并且厌倦了)试图解决这个问题。
哦,这是我第一次从标准WebForms构建MVC表单......
有什么想法吗?
谢谢!
答案 0 :(得分:1)
我打算发表评论,但是时间过长了。
您需要发布Requestform.cshtml
帖子的操作,此处也不需要您的帐户控制器代码。我的猜测是你的托管环境是共享托管,有些东西阻止你的电子邮件代码运行。
您可能要做的第一件事是,如果您的电子邮件地址位于try catch
内,请暂时评论我们的try catch
,这样就会抛出错误。这应该至少可以帮助您了解正在发生的事情。
接下来要做的是尝试为mvc3添加Elmah
,你可以通过Visual Studio中的Nuget添加它。确保使用Elmah for MVC3,因为它将主要为您设置一切。这将允许您看到它有任何错误被抛出。
否则,如果没有看到您的实际电子邮件代码,我将无法提供更多帮助。