ASP.NET MVC邀请系统

时间:2010-01-12 08:15:12

标签: c# asp.net-mvc

我需要在基于ASP.NET MVC的网站上支持邀请,以便成员可以邀请朋友加入。

是否存在可以为我执行此操作的现成组件,而不是从头开始?

2 个答案:

答案 0 :(得分:3)

我不知道任何像ASP.NET MVC那样的“即插即用”系统,但无论如何你都可以轻松实现一个基本系统。

像这样创建一个InviteController:

using System.Net.Mail;

namespace InTouch.Controllers
{

public class YourApp.Controllers
{
    public ActionResult Index()
    {
        return View();
    }

    [AcceptVerbs("POST")]
    public ActionResult Index(string fromname, string fromemail, string toname, string toemail)
  {
  const string emailregex = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
  var result = false;
  ViewData["fromname"] = fromname;
  ViewData["fromemail"] = fromemail;
  ViewData["toname"] = toname;
  ViewData["toemail"] = toemail;

  if (string.IsNullOrEmpty(fromname)) ViewData.ModelState.AddModelError("name", "Please enter your name!");
  if (string.IsNullOrEmpty(fromemail)) ViewData.ModelState.AddModelError("email", "Please enter your e-mail!");
  if (!string.IsNullOrEmpty(fromemail) && !Regex.IsMatch(fromemail, emailregex)) ViewData.ModelState.AddModelError("email", "Please enter your e-mail!");
  if (string.IsNullOrEmpty(toname)) ViewData.ModelState.AddModelError("comments", "Please enter a message!");
  if (!string.IsNullOrEmpty(toemail) && !Regex.IsMatch(toemail, emailregex)) ViewData.ModelState.AddModelError("email", "Please enter a valid recipient e-mail!");
  if (!ViewData.ModelState.IsValid) return View();

  var message = new MailMessage(fromemail, toemail)
        {
            Subject = "You have been invited to MyNewApp by " + fromname + "!",
            Body = fromname + " wants to invite you. Click my link httpwwwblahblah to join them!"
        };

        SmtpClient smtp = new SmtpClient();
        try
        {
            smtp.Send(message);
            result = true;
        }
        catch { }           

  return View("Thankyou");
    }


}
}

然后你只需要一个表格视图。这样的东西,符合你的口味:

<form id="invite" method="post">
<fieldset><legend>Invite a friend!</legend>
<%=Html.ValidationMessage("fromname")%>
<%=Html.ValidationMessage("fromemail")%>
<%=Html.ValidationMessage("toname")%>
<%=Html.ValidationMessage("toemail")%>
Your Name: <input type="text" id="fromname" name="fromname" class="required" value="<%= ViewData["fromname"] ?? "" %>" /><br />
Your Email: <input type="text" id="fromemail" name="fromemail" class="required" value="<%= ViewData["fromemail"] ?? "" %>" /><br />
Friend's Name: <input type="text" id="toname" name="toname" class="required" value="<%= ViewData["toname"] ?? "" %>" /><br />
Friend's Email: <input type="text" id="toemail" name="toemail" class="required" value="<%= ViewData["toemail"] ?? "" %>" /><br />
<input type="submit" id="action" name="action" value="Submit" />
</fieldset></form>

应该可以在不使应用程序的其余部分复杂化的情况下完成任务!

答案 1 :(得分:0)

首先,有一些提供商可以与WebServices和其他通信方法进行非常好的交互。尝试搜索适合您需求的Invite API。我不会像其他人所说的那样设置一个“通过电子邮件邀请”控制器,自己做邮件时会有“危险”。

假设您正在开发的页面偷看并拥有大量访问者,例如2000名访问者,他们所有人都想邀请10位朋友,现在有2万人邀请。如果您要从服务器请求20 000个SMTP发送,很多服务器会将您列入黑名单,现在这不好。

因此,您需要创建一个更高级别的邀请流程,您可以将所有邀请存储在数据库中并使用调度程序每分钟发送10封邮件,或使用可以处理大量邀请的第三方提供商。

永远不要想到小

如果你想通过facebook,myspace,twitter或者你想到的任何东西来邀请,那么就有那些难以管理的API。

相关问题