在我的通用处理程序中,我使用RestSharp执行异步HTTP POST,然后在等待POST请求返回之前向用户显示一些html。然后,一旦Http Response返回,我想通过注入一些Javascript来显示消息。
var asyncHandle = client.ExecuteAsync(request, response =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
//here I would like to display a notification to the user but the handler already returned processedHtml at this point
context.Response.Write(@"<script type='text/javascript'>
window.parent.$.gritter.add({
text: 'some message',
time: 20000,
sticky: false
});</script>");
}
});
//this line executes before waiting for response from async call
context.Response.Write(processedHtml);
问题在于,此时生命周期已经结束,因此context.Response.Write(@“&lt; script ...”)生成“未将对象引用设置为对象的实例”。当异步响应回来时,还有其他方法可以注入javascript吗?