gpt4 book ai didi

c# - ASP.NET MVC 邀请系统

转载 作者:行者123 更新时间:2023-11-30 22:47:56 24 4
gpt4 key购买 nike

我需要在我的 ASP.NET MVC 中支持邀请基于站点,因此成员可以邀请 friend 加入。

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

最佳答案

我不知道有任何类似 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");
}


}
}

然后您只需要一个表单 View 。像这样的东西,根据您的口味设计:

<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>

应该在不使应用的其余部分复杂化的情况下完成这个技巧!

关于c# - ASP.NET MVC 邀请系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2047566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com