gpt4 book ai didi

c# - ASP.NET MVC 中回调后的 RedirectToAction

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

我想在完成一些任务(发送电子邮件)后重定向到操作,但我不知道如何正确地做到这一点。

这是我的代码,但是 RedirectToAction 在这里没有做任何事情!

[HttpPost]
public ActionResult SendEmail(EmailContentViewModel emailDetails)
{
using (MailMessage email = new MailMessage(emailDetails.from, emailDetails.to))
{
email.Subject = emailDetails.subject;
email.Body = emailDetails.body;
email.Priority = emailDetails.MailPriority;

processSendingEmail(email, (result) =>
{
RedirectToAction("ContactResult", "Contact", new { success = result }); //It's not redirecting to the ContactResult page!
});
}

return null;
}

private void processSendingEmail(MailMessage email, Action<bool> callback= null)
{
using (SmtpClient smtpClient = new SmtpClient(_smtpHostName, _smtpPort))
{
bool sentSuccessfully = false;

try
{
//.............//
}
catch(Exception e)
{
//.............//
}

callback?.Invoke(sentSuccessfully);
}
}

基于 Panagiotis Kanavos's response ,这是一个工作代码:

    [HttpPost]
public async Task<ActionResult> SendEmail(EmailContentViewModel emailDetails)
{
using (MailMessage email = new MailMessage(emailDetails.from, emailDetails.to))
{
email.Subject = emailDetails.subject;
email.Body = emailDetails.body;
email.Priority = emailDetails.MailPriority;

var sentSuccessfully= await processSendingEmail(email);

return RedirectToAction("ContactResult", "Contact", new { success = sentSuccessfully});
}
}


private async Task<bool> processSendingEmail(MailMessage email)
{
var client = new MailKit.Net.Smtp.SmtpClient();
//Configure the client here ...
try
{
var msg = (MimeKit.MimeMessage)email;
await client.SendAsync(msg);
return true;
}
catch (Exception ex)
{
Debug.Fail(ex.Message);
string errorMessage = "";
switch (ex)
{
case SmtpFailedRecipientException f:
errorMessage = $"Failed to send to {f.FailedRecipient}";
break;
case SmtpException s:
errorMessage = "Protocol error";
break;
default:
errorMessage = "Unexpected error";
break;
}

//Do anything you want with the error message

return false;
}
}

最佳答案

不要使用回调。 RedirectToAction 创建一个应由操作返回的 ActionResult,它不会强制重定向。

异步执行某些操作的正确方法是使用 async/await。即使您的电子邮件库没有基于任务的异步方法,您也可以使用 TaskCompletionSource 使其适应基于任务的模型。这将是相当不寻常的,因为大多数库已经从旧的异步模型(如回调、事件和 APM)转移到任务。

MailMessage 表明您正在使用 SmtpClient . SendMailAsync方法是基于任务的,这意味着你可以写

await client.SendMailAsync(email);

例如:

[HttpPost]
public async Task<ActionResult> SendEmail(EmailContentViewModel emailDetails)
{
SmptClient client = ... //Configure the client here
using (MailMessage email = new MailMessage(emailDetails.from, emailDetails.to))
{
email.Subject = emailDetails.subject;
email.Body = emailDetails.body;
email.Priority = emailDetails.MailPriority;

await client.SendMailAsync(email);
return RedirectToAction("ContactResult", "Contact", new { success = true });
};
}

不过,SmptClient 是一个过时的类。它的文档页面警告说:

We don't recommend that you use the SmtpClient class for new development. For more information, see SmtpClient shouldn't be used on GitHub.

该链接解释说:

SmtpClient doesn't support many modern protocols. It is compat-only. It's great for one off emails from tools, but doesn't scale to modern requirements of the protocol.

建议使用较新的库,如 MailKit

MailKit 允许将 MailMessage 显式转换为 MimeMessage,这使得将现有代码转换为 MailKit 变得容易:

[HttpPost]
public async Task<ActionResult> SendEmail(EmailContentViewModel emailDetails)
{
var client = new MailKit.Net.Smtp.SmptClient();
/Configure the client here ...
using (MailMessage email = new MailMessage(emailDetails.from, emailDetails.to))
{
email.Subject = emailDetails.subject;
email.Body = emailDetails.body;
email.Priority = emailDetails.MailPriority;

var msg=(MailKit)email;
await client.SendAsync(msg);
return RedirectToAction("ContactResult", "Contact", new { success = true });
};
}

错误处理

MailKit 和旧的 SmptClient 的 Send 方法都成功或抛出。一种选择是隐藏异常并返回真/假成功标志:

try
{
await client.SendAsync(msg);
return RedirectToAction("ContactResult", "Contact", new { success = true});
}
catch
{
return RedirectToAction("ContactResult", "Contact", new { success = false});
}

但这对尝试诊断可能问题的用户或管理员来说不是很有帮助。这些方法的文档解释了可能发生的异常类型,例如:ArgumentNullException 表示空消息、InvalidOperationException、SmtpFailedRecipientException 等等。

至少,代码可以在返回失败之前记录异常:

catch(Exception ex)
{
_log.Error(ex);
return RedirectToAction("ContactResult", "Contact", new { success = false});
}

一个更好的主意是处理特定的异常并可能警告用户:

catch(SmtpFailedRecipientException ex)
{
_log.Error(ex);
return RedirectToAction("ContactResult", "Contact", new { success = false,message=$"Couldn't send the message to {ex.FailedRecipient}"});
}
catch(SmtpException ex)
{
_log.Error(ex);
return RedirectToAction("ContactResult", "Contact", new { success = false,message="Failed to send the message"});
}
catch(Exception ex)
{
_log.Error(ex);
return RedirectToAction("ContactResult", "Contact", new { success = false,message="An unexpected error occured"});
}

C# 7 中的模式匹配使这更容易:

catch(Exception ex)
{
_log.Error(ex);
string message="";
switch (ex)
{
case SmtpFailedRecipientException f:
message=$"Failed to send to {f.FailedRecipient}";
break;
case SmptException s :
message="Protocol error";
break;
default:
message="Unexpected error";
break;
}
return RedirectToAction("ContactResult", "Contact", new { success = false,message=message});
}

分离方法

将发送代码重构为单独的方法很容易。 try/catch block 和客户端声明可以提取到单独的方法中:

async Task<string> MySendMethod(MailMessage email)
{
var client = new MailKit.Net.Smtp.SmptClient();
//Configure the client here ...
try
{
var msg=(MailKit)email;
await client.SendAsync(msg);
return "";
}
catch(Exception ex)
{
_log.Error(ex);
switch (ex)
{
case SmtpFailedRecipientException f:
return $"Failed to send to {f.FailedRecipient}";
case SmptException s :
return "Protocol error";
default:
return "Unexpected error";
}
}
}

该方法不返回 RedirectToActionResult,而是返回结果字符串。如果为空,则操作成功。 Controller Action 可以这样重写:

[HttpPost]
public async Task<ActionResult> SendEmail(EmailContentViewModel emailDetails)
{
using (MailMessage email = new MailMessage(emailDetails.from, emailDetails.to))
{
email.Subject = emailDetails.subject;
email.Body = emailDetails.body;
email.Priority = emailDetails.MailPriority;

var message=await MySendMethod(email);
return RedirectToAction("ContactResult", "Contact",
new { success = String.IsNullOrWhitespace(result),
message=message
});
};
}

关于c# - ASP.NET MVC 中回调后的 RedirectToAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54713027/

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