Monday, September 14, 2009

Old Skool - Classic ASP ServerVariables List

This is an oldie but a goodie – I don’t know how many times in my career I’ve rewritten these few lines of code but I’ve preserved it here to save me writing it yet again! A simple method for outputting all the server variables, query string parameters and form variables in Classic ASP!

response.Write("<hr/>SERVER VARIABLES COLLECTION<br/>")
Dim variableName
for each variableName in Request.ServerVariables
response.write(variableName & ": " & Request.ServerVariables(variableName) & "<br/>")
next
response.Write("<hr/>QUERY STRING COLLECTION<br/>")
for each variableName in Request.QueryString
response.write(variableName & ": " & Request.QueryString(variableName) & "<br/>")
next
response.Write("<hr/>FORM COLLECTION<br/>")
for each variableName in Request.Form
response.write(variableName & ": " & Request.Form(variableName) & "<br/>")
next
response.Write("<hr/>SESSION COLLECTION<br/>")
for each variableName in Session.Contents
response.write(variableName & ": " & Session(variableName) & "<br/>")
next
response.Write("<hr/>")

Read More

Tuesday, September 01, 2009

Useful code to properly record and redirect an unhandled exception from Global.asax Application_Error Handler

    void Application_Error(object sender, EventArgs e)

    {

        // Code that runs when an unhandled error occurs

        string httpCode = ((HttpException) Server.GetLastError()).GetHttpCode().ToString();

        if (HttpContext.Current.Session != null)

        {

            HttpContext.Current.Session["LastError"] = Server.GetLastError();

            Server.ClearError();

        }

        switch (httpCode)

        {

            case "404":

                Response.Redirect("/Error/Error404.aspx");

                break;

            default:

                Response.Redirect("/Error/Error.aspx");

                break;

        }

    }

Read More