Reputation: 8526
I am sending emails asynchronous using ASP.NET. I noticed that I have to set "Async = true" in the View.
The master page doesn't support this property. How can I set Async for all pages?
Upvotes: 5
Views: 16072
Reputation: 614
As explained in the original answer, you can force the AsyncMode
to true inside a parent class, but you also have to implement IAsyncHttpHandler
.
This interface is auto-implemented in the generated code of your page if you set Async="true"
in your aspx, but not if AsyncMode
is set from the constructor.
public abstract class BasePage : System.Web.UI.Page, System.Web.IHttpAsyncHandler
{
public BasePage()
{
AsyncMode = true;
}
public virtual IAsyncResult BeginProcessRequest(System.Web.HttpContext context, AsyncCallback cb, object data)
{
return AsyncPageBeginProcessRequest(context, cb, data);
}
public virtual void EndProcessRequest(IAsyncResult ar)
{
AsyncPageEndProcessRequest(ar);
}
}
And then inherit from this class for all your pages:
public partial class MyPage : BasePage
Upvotes: 0
Reputation: 858
You can use this strategy if you have pages that don't have a codebehind.
namespace com.mystuff
{
public class MyPage: System.Web.UI.Page
{
public MyBasePage()
{
this.AsyncMode = true;
}
}
}
<system.web>
<pages pageBaseType="com.mystuff.MyPage" />
</system.web>
Upvotes: 0
Reputation: 2729
Include tpl references. Then do Task.Run(()=> Sendmailmethod()); This is the quick n neat fire n forget pattern
Upvotes: 0
Reputation: 157038
You can set it in the master page like this. Found solution here:
public abstract class MyBasePage : System.Web.UI.Page
{
public MyBasePage()
{
this.AsyncMode = true;
}
}
Then change the inheritance in the aspx.cs file to something like this:
public partial class WebForm1 : MyBasePage
It can break the system when you set the AsyncMode
property in anything else then the constructor.
Upvotes: 1
Reputation: 446
Rather than trying to send emails in the page code of ASP.NET, why not use AJAX? Make an ASPX file that responds to a POST like a login form and when POSTed, it sends an email. AJAX is asynchronous and you can display progress in the active web page using it. An intro to AJAX can be found here: http://www.w3schools.com/ajax/default.asp
Personally, I use jQuery to do my AJAX work. You may want to check it out: http://jquery.com/
And last but not least, if you'd rather not "roll-your-own" code to do this, check out a Web API for mail delivery like Mandrill: http://mandrill.com/
Upvotes: 0
Reputation: 50728
Open the find dialog and enter:
Find: @Page
Replace with: @Page Async="True"
Click the Replace All button :-D To my understanding, you can't just do that automatically for everything. It needs to be defined on each and every page.
Upvotes: 2