Response.Redirect to Class that inherits from UI.P

2019-05-25 21:36发布

问题:

everyone, thank for your time.

Well this my problem (well it's not a probleam at all), it is possible to have a class that inherits from ui.page and then instance an object of that class and do a redirect to it ?

Something like this:

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

I do this in my webForm (and that what I want to do in a class that inherits from 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()

What I want to do is perfectly possible with a normal WebForm, but my webForm doesn't render nothing at all (at least as (x)html so, that's because I would like to know if what I'm asking is possible to achieve.

Thank you everyone.

Well at the end I just add "HttpContext.Current." to all the lines that include a "response" attribute, so now I have just a class that NOT inherits from "UI.Page" and just call the method that clear the buffer (a custom method), add the headers (to force the download,set the mime type and filename) and flush the response by itself and get the effect/use I want it.

In order to access to the Session vars just add "HttpContext.Current." and it works, I don't know how secure or if is a good way,but appears to work well.

So the code now looks something like this:

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

And from a command for example do this:

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

Of course now you can add more attributes or method if you need it.

So now I don't even don't use Response.redirect() or inherits from "UI.Page", just a class that "change" the "current response" and "flush" the content created on fly with the crystal report, I think I was kind of totally lost but your answers really help me, especially what Tejs says, what is almost the same or the same what I just did. Thank you.

UPDATE: By the way, I just realize that the ReportDocument class has this method: ExportToHttpResponse that let us flush the Crystal Report to the browser as PDF/XSL etc forcing (or not) the download of the file.

回答1:

Try just doing this instead:

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


回答2:

No, as there is no current overload that accepts a UI.Page instance. However, you could call the Render method on your new page and write directly to the response stream. AKA

  1. Clear the Response Buffer.
  2. Render your page instance to the response buffer.
  3. Invoke Response.End() to flush the request and send it to the client.

If your new page doesnt actually do anything, you can additional just send no content back with the response.



回答3:

You can use Server.Transfer and pass in the instance of the page object that you want to tranfer to.

Here is the documentation: HttpServerUtility.Transfer



回答4:

 HttpRequest Request = HttpContext.Current.Request;
 HttpResponse Response = HttpContext.Current.Response; 

And after that you can redirect any page.