I'm using ASP.NET MVC3
I have a .cshtml
view and I want to stringify it to be incorporated in an email body.
Here is the method I use :
//Renders a view to a string
private string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new System.IO.StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
When I call this method from an ActionResult
method which is called from an Ajax call, this perfectly works.
However, I'm facing an unusual situation :
In my Global.asax
file, I have a method called every 10 minutes whose goal is to verify if some special records have been made in database these last 10 minutes, and if so, sends an email. Of course, the email body is this stringified view.
Here is a piece of my code : This method is very inspired of this post
/* File : Gloabal.asax.cs */
private static CacheItemRemovedCallback OnMatchingCacheRemove = null;
protected void Application_Start()
{
// ...
AddMatchingTask("SendEmail", 600);
}
private void AddMatchingTask(string name, int seconds)
{
OnMatchingCacheRemove = new CacheItemRemovedCallback(CacheItemMatchingRemoved);
HttpRuntime.Cache.Insert(name, seconds, null, DateTime.UtcNow.AddSeconds(seconds), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, OnMatchingCacheRemove);
}
//This method is called every 600 seconds
public void CacheItemMatchingRemoved(string k, object v, CacheItemRemovedReason r)
{
using (MyEntities context = new MyEntities())
{
var qMatching = from m in context.MY_TABLE
where m.IsNew == true
select m;
if (qMatching.Any())
{
MatchingController matchingController = new MatchingController();
matchingController.SendEmail();
}
}
// re-add our task so it recurs
AddMatchingTask(k, Convert.ToInt32(v));
}
The SendEmail()
method should create the body of the email, getting the view and putting it in an HTML string to send
public void SendEmail()
{
/* [...] Construct a model myModel */
/* Then create the body of the mail */
string htmlContent = RenderRazorViewToString("~/Views/Mailing/MatchingMail.cshtml", myModel);
}
Here, RenderRazorViewToString()
(the method's body is given at the top of this post) fails at this line :
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ControllerContext cannot be null
Why, in this case only, ControllerContext
is null
?
I have read this post, but if I understood it correctly, this is because I manually instantiated my Controller writing :
MatchingController matchingController = new MatchingController();
However, I don't know how to proceed otherwise...
Any help will be very appreciated.
Thank you