In our ItemAdded event receiver code, we would like to call
Server.Execute(new MyPage(), writer, false)
to statically generate the HTML code for MyPage and store it in the RootFolder so client can access it via URL.
However, HttpContext.Current is null at the start of the ItemAdded method. I found a reference that suggested to do use the following code to create a valid HttpContext:
StringBuilder sb = new StringBuilder();
using (StringWriter writer = new StringWriter(sb))
{
var request = new HttpRequest("", web.Url, "");
HttpContext.Current = new HttpContext(request, new HttpResponse(writer));
HttpContext.Current.Server.Execute(new MyPage(), writer, false);
}
string formContent = sb.ToString();
However, when executing this code, I get an HttpException with an NullReferenceException InnerException in System.Web.UI.Page.SetIntrinsics().
Is this the correct way to execute a server page and return the generated contents as a string? If so, what am I missing to get the above code working? If not, what's the correct way to accomplish this task?
Update Our server needs to be configured with Basic Auth (since iOS browsers will not respond to a 401 challenge by Windows Auth when asked to download a cache.manifest file). This in turn would require us to use explicit name/password in our WebClient.DownloadString() call, something we don't have access to in our event handler code.