简体   繁体   English

ASP.NET MVC:发送响应后如何进行后期操作?

[英]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.

相关问题 如果字段值已无效,如何将其保留在ASP.NET MVC表单中? - How do I keep field values in the ASP.NET MVC form if it has been invalidated? 如何在页面重定向后发布消息以确认已发送的消息(C#/ ASP.Net) - How do I post a message to confirm a sent message after a page redirect (C#/ASP.Net) ASP.NET MVC:如何在页面加载后更改Ajax.BeginForm将发布到的位置? - ASP.NET MVC: How do I change where Ajax.BeginForm will post to after the page loads? 如何在asp.net mvc中回复后清除字段? - How to clear fields after post back in asp.net mvc? 在ASP.NET MVC中发布后如何更改表单值? - How to change form values after post in ASP.NET MVC? 仅在提交搜索后显示部分视图Asp.Net MVC - Showing partial-view, only after search has been submitted Asp.Net MVC 使用Ninject的ASP.NET MVC 5 SiteMapLoader尚未初始化 - ASP.NET MVC 5 using Ninject | The SiteMapLoader has not been initialized ASP.NET MVC:控制器如何区分URL中的参数和POST发送的参数 - ASP.NET MVC: How does a controller distingush between parameters in the URL and sent by POST asp.net mvc构建操作后更新网站? - asp.net mvc update website after build operation? ASP.NET MVC如何找到哪个属性出错 - ASP.NET MVC How can I find which attribute has been error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM