[英]ASP.NET MVC: How to do post operation after response has been sent?
I have a controller that is responsible for persisting a record in DB, upon success, it has to send an email to Admin. 我有一个控制器,负责将记录持久存储在数据库中,成功后,它必须向Admin发送电子邮件。 This end point is used by APP and sending email before API response can increase wait time.
APP使用此端点,并在API响应可以增加等待时间之前发送电子邮件。 So instead of sending email after record has been persisted, I want to send after controller sends the response to APP.
因此,我想在控制器将响应发送到APP之后发送,而不是在记录已保存后发送电子邮件。
I googled for it but not able to find concrete answer. 我用谷歌搜索,但找不到具体答案。 I thought starting point could be action filter.
我以为起点可以是动作过滤器。
Thank you. 谢谢。
我将实现一个通知服务,仅将消息添加到队列中,这样控制器就不必等待电子邮件发送了。
Maybe you can use ResultFilter: 也许您可以使用ResultFilter:
public class MailSenderAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
//your code...
}
}
...
[MailSenderAttribute]
public ActionResult YourAction() { /*code*/ }
Because after response is sent from controller to client, MVC lifecycle will end so action filters does not help. 因为从控制器向客户端发送响应后,MVC生命周期将结束,所以操作筛选器无济于事。 Also agree with NachoMK.
也同意NachoMK。 I think we can implement something as suggested in this blog by Scott Hanselman.
我认为我们可以实现Scott Hanselman在此博客中建议的方法。 http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx
http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.