【问题标题】:Response.Redirect to Class that inherits from UI.Page?Response.Redirect 到继承自 UI.Page 的类?
【发布时间】:2012-04-19 20:59:16
【问题描述】:

大家,感谢您的宝贵时间。

这是我的问题(这根本不是问题),有可能有一个继承自 ui.page 的类,然后实例化该类的一个对象并重定向到它?

类似这样的:

Public sub myMethod() 
    Dim myPage as new myClassThatInheritsFromUIPage()
    Response.redirect(myPage) 'Here is one of my "no-idea" line
end sub

我在我的 webForm 中执行此操作(以及我想在继承自 ui.page 的类中执行此操作):

Response.BufferOutput = True  
Response.ClearContent()
Response.ClearHeaders()
ostream=crReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
Response.AppendHeader("Cache-Control", "force-download")
Response.AppendHeader("Content-disposition","attachment;filename=ReportAsPDF.pdf")
Response.ContentType = "application/pdf"
Response.BinaryWrite(ostream.ToArray())
Response.Flush()  
HttpContext.Current.ApplicationInstance.CompleteRequest()

使用普通的 WebForm 完全可以实现我想要做的事情,但是我的 webForm 根本不呈现任何内容(至少作为 (x)html 如此,那是因为我想知道我要问的是有可能实现。

谢谢大家。

最后我只是将 "HttpContext.Current." 添加到包含“响应”属性的所有行中,所以现在我只有一个不继承自“UI.Page”的类" 并且只需调用清除缓冲区的方法(自定义方法),添加标题(强制下载,设置 mime 类型和文件名)并自行刷新响应并获得我想要的效果/使用它。

为了访问会话变量,只需添加“HttpContext.Current”。并且它有效,我不知道有多安全或是否是一种好方法,但似乎运作良好。

所以代码现在看起来像这样:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.ReportSource

Namespace foo
  Public Class requestReport


    'just to instance the object'
    Public Sub New()


    End Sub


    Public Sub downloadReport()
     'do all the stuff to connect and load the report'
         HttpContext.Current.Response.BufferOutput = True  
         HttpContext.Current.Response.ClearContent()
         HttpContext.Current.Response.ClearHeaders()
  ostream=crReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
        HttpContext.Current.Response.AppendHeader("Cache-Control", "force-download")
        HttpContext.Current.Response.AppendHeader("Content-disposition","attachment;filename=ReportAsPDF.pdf")
        HttpContext.Current.Response.ContentType = "application/pdf"
        HttpContext.Current.Response.BinaryWrite(ostream.ToArray())
        HttpContext.Current.Response.Flush()  
        HttpContext.Current.ApplicationInstance.CompleteRequest()
     End Sub
   End Class 
 End Namespace

例如,通过命令执行以下操作:

dim myReport as new foo.requestReport()
myReport.downloadReport() 

当然现在你可以根据需要添加更多的属性或方法。

所以现在我什至不使用 Response.redirect() 或从“UI.Page”继承,只是一个“改变”“当前响应”和“刷新”的类“使用水晶报告即时创建的内容,我想我有点迷失了,但你的回答真的帮助了我,尤其是 Tejs 所说的,我刚刚做的几乎相同或相同.谢谢。

更新: 顺便说一句,我刚刚意识到 ReportDocument 类有这个方法:ExportToHttpResponse,它让我们将 Crystal Report 作为 PDF/XSL 等强制(或不强制)下载文件到浏览器。

【问题讨论】:

  • 你在正确的轨道上。试试吧!

标签: asp.net code-behind response.redirect


【解决方案1】:

不,因为当前没有接受 UI.Page 实例的重载。但是,您可以在新页面上调用 Render 方法并直接写入响应流。又名

  1. 清除响应缓冲区。
  2. 将您的页面实例呈现到响应缓冲区。
  3. 调用Response.End() 刷新请求并将其发送给客户端。

如果您的新页面实际上没有做任何事情,您可以额外只发送不包含响应的内容。

【讨论】:

  • 我不明白,我在我的对象上看不到“渲染”方法。请您扩展您的答案,如果我这样做,“实际”响应会丢失吗?我从继承的类中的对象实际上是一个供下载的文件(xls,pdf)(那是因为我根本不想使用 webForm)我在内部使用 Crystal 报表导出方法“即时”创建文件,它可以工作使用 webForm(更改标题,刷新响应),但我正在尝试使用仅从 ui.page 继承的类,这是个好主意还是我完全错了?
  • 我明白了,我只是使用与 webForm 上相同的方法,但对于所有带有或涉及“response”属性的行添加“HttpContext.Current”。
【解决方案2】:

您可以使用 Server.Transfer 并传入您要传输到的页面对象的实例。

这是文档:HttpServerUtility.Transfer

【讨论】:

  • 您的回答看起来很有用,但我认为这对我的问题来说“太强大了”,实际上就在几天前,我想知道如何做到这一点(“自定义”httpRequest)。谢谢。
【解决方案3】:

尝试这样做:

HttpContext.Current.Response.Redirect("...");

【讨论】:

    【解决方案4】:
     HttpRequest Request = HttpContext.Current.Request;
     HttpResponse Response = HttpContext.Current.Response; 
    

    然后你可以重定向任何页面。

    【讨论】:

    • HttpRequest 请求 = HttpContext.Current.Request; HttpResponse 响应 = HttpContext.Current.Response;
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多