HttpModule中的Response.Redirect

时间:2011-12-12 09:24:19

标签: c# httpmodule

我正在尝试从HttpModule重定向到网址,似乎没有任何效果,我尝试了以下

(sender as HttpApplication).Context.RewritePath(url);
(sender as HttpApplication).Response.Redirect(url, true);

有没有人知道正确的代码?

这是HttModule的代码,我在CMS中拦截对某些PDF文档的请求,并将日期添加到PDF中,将其保存到文件系统,然后尝试重定向到它

private void Application_EndRequest(object sender, System.EventArgs e)
{
    HttpContext context = HttpContext.Current;
    HttpApplication appContext = sender as HttpApplication;

    ADXSTUDIO.Documents.Document document = CmsContext.Current.Document;

    if (document != null && document.Type.Equals("adxResource.1"))
    {
        if (document.GetAttributeValue<bool>("AppendDateToPDF") && HttpContext.Current.Request.QueryString["a"] == null)
        {
            context.Response.Clear();

            string issueDate = "Issue Date: " + DateTime.Now.ToString("HH:mm dd/MM/yyyy");

            Guid id = Guid.NewGuid();
            string filepath1 = HttpContext.Current.Server.MapPath(string.Format("~/appforms/{0}_template.pdf", id));
            string filepath2 = HttpContext.Current.Server.MapPath(string.Format("~/appforms/{0}.pdf", id));
            string url = string.Format("/appforms/{0}.pdf", id);

            PdfReader reader = new PdfReader(document.Url + "&a=1");
            PdfStamper stamper = new PdfStamper(reader, new FileStream(filepath1, FileMode.Create));
            AddVerticalTextBox(stamper, "PageOneDownloadDate", 1);
            stamper.Close();
            reader.Close();

            reader = new PdfReader(filepath1);
            stamper = new PdfStamper(reader, new FileStream(filepath2, FileMode.Create));
            stamper.AcroFields.SetField("PageOneDownloadDate", issueDate);
            stamper.FormFlattening = true;
            stamper.FreeTextFlattening = true;
            stamper.Close();
            reader.Close();

            File.Delete(filepath1);

            //context.RewritePath(url);
            (sender as HttpApplication).Context.RewritePath(url);
            //(sender as HttpApplication).Response.Redirect(url, true);
        }
    }
}

0 个答案:

没有答案