DeveloperPanel Class - ASP.NET

This page is a Draft. Its content is not complete and might contain errors.

This page is part of the Class Library Pages collection.
Click the icon to see the index.

Source Code

VB.NET

{copytext|SourceVb}
Imports System.Web.UI.WebControls

Namespace ABC

    Public Class DeveloperPanel
        Inherits Panel

        Public Sub New()

            Dim t As Table = New Table()
            Dim r As HttpRequest = HttpContext.Current.Request
            t.CellPadding = 1
            t.BorderWidth = New Unit(0, UnitType.Pixel)
            t.Rows.Add(NewRow("Server", Environment.MachineName))
            t.Rows.Add(NewRow("Time", DateTime.Now.ToString("MM/dd/yy h:mm tt")))
            t.Rows.Add(NewRow("Conn", SqlDatabaseWeb.GetConnName("MYDB")))
            t.Rows.Add(NewRow("Client", r.UserHostAddress))
            t.Rows.Add(NewRow(" ", r.UserHostName))
            t.Rows.Add(NewRow("User", r.LogonUserIdentity.Name))
            Me.Controls.Add(t)

        End Sub

        Private Function NewRow(ByVal label As String, ByVal value As String) As TableRow

            Dim result As TableRow = New TableRow()
            Dim cell As TableCell = New TableCell()
            cell.Text = label
            cell.Font.Bold = True
            cell.Font.Size = New FontUnit(8)
            result.Cells.Add(cell)
            cell = New TableCell()
            cell.Text = value
            cell.Font.Size = New FontUnit(8)
            result.Cells.Add(cell)
            Return result

        End Function

    End Class

End Namespace

C#

{copytext|SourceCs}
private const string dateFormat = "MM/dd/yyyy hh:mm tt (zzz)";
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Table t = uxTable;
        HttpRequest r = HttpContext.Current.Request;
        t.CellPadding = 1;
        t.BorderWidth = new Unit(1, UnitType.Pixel);
        t.Rows.Add(NewRow("URL:", AppRootUrl()));
        t.Rows.Add(NewRow("Server:", HttpContext.Current.Server.MachineName));
        t.Rows.Add(NewRow("Server IP:", GetServerIP()));
        t.Rows.Add(NewRow("App Root:", Server.MapPath("~/")));
        t.Rows.Add(NewRow("Deployed:", DeploymentTimestamp()));
        t.Rows.Add(NewRow("Server Time:", DateTime.Now.ToString(dateFormat)));
        t.Rows.AddRange(NewRows("Database Connections:", GetConnections()));
        t.Rows.Add(NewRow("Client:", r.UserHostAddress));
        t.Rows.Add(NewRow(" ", r.UserHostName));
        t.Rows.Add(NewRow("OS User:", r.LogonUserIdentity == null ? "" : r.LogonUserIdentity.Name));
        t.Rows.Add(NewRow("ASP.NET User:", GetAspNetUserName()));
    }
}
private TableRow NewRow(string label, string value)
{
    TableRow result = new TableRow();
    TableCell cell = new TableCell();

    cell.Text = label;
    cell.Font.Bold = true;
    cell.Font.Size = new FontUnit(10);
    cell.VerticalAlign = VerticalAlign.Top;
    cell.HorizontalAlign = HorizontalAlign.Right;
    cell.Width = new Unit(12, UnitType.Em);
    result.Cells.Add(cell);
    cell = new TableCell();
    cell.Text = value;
    cell.Font.Size = new FontUnit(10);
    cell.VerticalAlign = VerticalAlign.Top;
    cell.ColumnSpan = 2;
    result.Cells.Add(cell);
    return result;
}
private TableRow[] NewRows(string label, Dictionary<string, string> items)
{
    List<TableRow> result = new List<TableRow>();
    TableCell cell;
    TableRow row;
    bool first = true;

    foreach (string key in items.Keys)
    {
        row = new TableRow();

        /*--- Cell #1 ---*/
        cell = new TableCell();
        if (first)
            cell.Text = label;
        else
            cell.Text = " ";
        cell.Font.Bold = true;
        cell.Font.Size = new FontUnit(10);
        cell.VerticalAlign = VerticalAlign.Top;
        cell.HorizontalAlign = HorizontalAlign.Right;
        cell.Width = new Unit(12, UnitType.Em);
        row.Cells.Add(cell);

        /*--- Cell #2 ---*/
        cell = new TableCell();
        cell.Text = key;
        cell.Font.Size = new FontUnit(10);
        cell.VerticalAlign = VerticalAlign.Top;
        cell.Width = new Unit(8, UnitType.Em);
        row.Cells.Add(cell);

        /*--- Cell #3 ---*/
        cell = new TableCell();
        cell.Text = items[key];
        cell.Font.Size = new FontUnit(10);
        cell.VerticalAlign = VerticalAlign.Top;
        row.Cells.Add(cell);

        /*--- Clean Up ---*/
        result.Add(row);
        first = false;
    }
    return result.ToArray<TableRow>();
}

private string GetServerIP()
{
    string result = "";
    IPAddress[] addresses = Dns.GetHostAddresses(HttpContext.Current.Server.MachineName);
    if (addresses.Length > 0)
    {
        result = addresses[0].ToString();
    }
    return result;
}

private string GetAspNetUserName()
{
    string result = "";
    result = HttpContext.Current.User.Identity.Name;
    return result;
}

private string ParseConnection(string conn)
{
    string result = "";

    //-- Handle Connection String for EntityFramework (?) ---
    Regex regex = new Regex("connection string=\"(?<conn>.+?)\"");
    MatchCollection mc = regex.Matches(conn);
    if (mc.Count > 0)
        conn = mc[0].Groups["conn"].Value;

    //-- Extract info of interest ---
    SqlConnectionStringBuilder b = new SqlConnectionStringBuilder(conn);
    result = "[" + b.DataSource + "].[" + b.InitialCatalog + "]";

    return result;
}
private string GetConnection(string key)
{
    string result = "";
    ConnectionStringSettings css = ConfigurationManager.ConnectionStrings[key];
    if (css != null)
        result = ParseConnection(css.ConnectionString);
    return result;
}
private Dictionary<string, string> GetConnections()
{
    Dictionary<string, string> result = new Dictionary<string, string>();
    ConnectionStringSettingsCollection items = ConfigurationManager.ConnectionStrings;

    /* Not sure where the "LocalSqlServer" connection keeps coming from, but we just
        exclude it here. */
    foreach (ConnectionStringSettings item in items)
        if (item.Name.ToUpper() != "LOCALSQLSERVER")
            result.Add(item.Name, ParseConnection(item.ConnectionString));

    return result;
}
private string AppRootUrl()
{
    string result = "";
    HttpRequest r = HttpContext.Current.Request;
    result += r.Url.Scheme;
    result += System.Uri.SchemeDelimiter;
    result += r.Url.Authority;
    result += r.ApplicationPath;
    if (result.EndsWith("/"))
        result = result.Substring(0, result.Length - 1);
    return result;
}

private string GetUrlRewriterConnection()
{
    string result = "";
    try
    {
        string configPath = Path.Combine(HttpContext.Current.Server.MapPath("~"), "web.config");
        XmlDocument doc = new XmlDocument();
        doc.Load(configPath);
        XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
        nsm.AddNamespace("url", "http://www.urlrewriting.net/schemas/config/2006/07");
        string xPath = "/configuration/url:urlrewritingnet/url:rewrites/url:add[@name='Rule1']";
        XmlNode node = doc.SelectSingleNode(xPath, nsm);
        string conn = node.Attributes["connString"].Value;
        result = ParseConnection(conn);
    }
    catch { }
    return result;
}

private string DeploymentTimestamp()
{
    string result = "";
    string path = Server.MapPath("~/bin");
    DirectoryInfo di = new DirectoryInfo(path);
    result = di.CreationTime.ToString(dateFormat);
    return result;
}