Search code examples
asp.netasp.net-mvc-3httpcontextmvcmailer

Using MvcMailer from Global.asax


Have developed an MVC3 application which monitors when users access static content on the web server, debiting the users balance each time. This logic happens in the "Application_AuthenticateRequest" event of Global.asax (I've read previously this is the correct place to put such logic).

MvcMailer has been implemented to send out various notifications to the user (account created, reset password etc). We'd like to implement a "low balance" notification, but don't seem to be able to send one at "Application_AuthenticateRequest" as no full HttpContext is available...? The full error which appears when "PopulateBody" is called is:

ArgumentNullException was unhandled by user code.
Value cannot be null.
Parameter name: routeData

Any suggestions how to work around this? Was thinking about sending notifications at another event at Global.asax, but will also need to fire for static content...?

Thanks in advance!


Solution

  • You could set something in the context for that request which you then read (or determine instead) in Application_EndRequest and send the request out from there. Better yet, simply store it in the cache and look for it on Application_EndRequest or make the entire determination on EndRequest if the count is high enough, etc.