REdirecting

  • Thread starter Thread starter Yama
  • Start date Start date
Y

Yama

Hi,

How can I redirect to an error page from a component vb.net?

Public Class bo

Protected Friend Function CreateString(ByRef lst As ListBox) As String

dim s As String



Try

s = "Hello World"

' create a new dataset

Catch ex As Exception

' How can I use Response.Redirect("page.aspx") from here???

Trace.Write(ex.Message)

Finally



End Try

Return s

End Function

End Class

Thanks,

Yama
 
Dont redirect from within the component. Such decisions belong in the upper
application layer. Simply raise the exception in the case below, and let
the layer above deal with how to report the error.
 
Hi Nick,

How would I know it threw an exception? How can I validate for it? I don't
want the web to crash. I would rather return a descriptive error message to
the user.

Yama
 
Yama Try this
Use
Response.Redirect("page.aspx",false)

Try

s = "Hello World"

' create a new dataset

Catch ex As Exception

' How can I use Response.Redirect("page.aspx") from here???

' you can use Response.Redirect("Page.aspx") here like this
Response.Redirect("Page.aspx",false)

Trace.Write(ex.Message)

Finally



End Try

Reason is Try & Catch will work under a thread. So if it will not execute end try this thread is not aborted. Before that if you try to redirect a open thread it will throw error. For aborting this single thread use Response.redirect's second parameter to false.

this will abort your try single thread and move on to redirected page.

Default Response.redirect has 2nd parameter as true.

if you want to see the detail of error case

you can do this


Response.redirect("page.aspx?errText=" & e.tostring",false)

in your page.aspx

you can print

Response.write(Request.querystring("errText")
 
Joji,

Thank you for your reply. But because I am trying to redirect from a class
that is not implemented with System.Web.UI.Page I do not have access to the
PAge namespace.

I tried some like this:

Private PageUI As System.Web.UI.Page
PageUI.Response.Redirect("gfdgdg.aspx")

But it didn't work!

The problem I am having is that I am trying to redirect directly from a
class. Is it at all possible?

Yama
 
Yama,

You must have made a call into your component/class from within a web page?
In the web page do the following:

public void SomeMethodCall()
{
try
{
YourClass class = someFactory.GetClass(); // However you acquire an
instance of the class
class.CallTheMethodYouNeed(withDataYouRequire);
}
catch(Exception e)
{
Response.Redirect("error.aspx", ....etc);
}
}

I am sure most here will agree that coupling your class to the web
infrastructure is simply not a good choice. It is best the class is
agnostic to the host (if it is a business component which is what I have
assumed). If you really do need to have the class perform the redirect,
pass in the HttpContext object to the business component.

HTH

Nick.
 
Hi all,

Well here is the way I settled for:
In my web.config file I added the following:
----------------------------------------------------------------------------
<customErrors mode="remoteOnly" defaultRedirect="error.aspx" />

<!-- You can configure the custom errors to be On, Off, or RemoteOnly. On
means everyone sees it. Off means no one sees the custom errors.
RemoteOnly means that any off the box will see the custom error page and
anyone on the box will see the real stack trace. -->

<error statusCode="404" redirect="NotFound.aspx" />
<error statusCode="403" redirect="AccessNotAllowed.aspx" />
<error statusCode="500" redirect="InternalError.aspx" />
<!-- etc... -->
 
Hi all,

Another good one is writing into your Global.asax file to handle an error
such as:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

Dim oMail As New MailMessage

oMail.To = "(e-mail address removed)"

oMail.Cc = "(e-mail address removed)"

oMail.From = "Admin"

oMail.Subject = "Unhandled Error!"

oMail.BodyFormat = MailFormat.Html

oMail.Body = "<html><body><h1>" & Request.Path & _

"</h1>" & "</body></html>"

SmtpMail.Send(oMail)

End Sub
 
Hi,

You may want to ADD the following lines of code:

Imports System.Web.Mail
SmtpMail.SmtpServer.Insert(0, "MyExchangeServerName")

before SmtpMail.Send(oMail)

Yama
 
Back
Top