How To Trigger Code Snippets Within Visual Studio

While typing a code statement that is recognized by Visual Studio to have a code snippet associated to it you can either TAB TAB or just hold down TAB.

In the case where there is something that can be populated like a switch statement the area will turn green. If you add an enum type to the green area then defocus out, it will autogenerate the case’s for each of the items in the enum.

Generate Full URL Path To Current Executing ASPX Excluding ASPX Filename

This will get you the full URL path to the current ASPX excluding the ASPX filename. Useful in cases where a relative path won’t work and you need the full hostname/path/.

So for example:
If this code is executing: http://www.domain.com/path/to/default.aspx –> http://www.domain.com/path/to/

// generate full URL path to aspx which does the work retrieving the TRI stock data
string StockPagePath = "http://" + HttpContext.Current.Request.Url.Host + 
Page.TemplateSourceDirectory.ToString() + "/TRIStockData.aspx";

Another example, also declaring page object

Page page = (Page)HttpContext.Current.CurrentHandler;

string errorUrl = "http://" + HttpContext.Current.Request.Url.Host + page.TemplateSourceDirectory.ToString() + "/error.aspx";

Another example including the port

string errorUrl = "http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port +  page.TemplateSourceDirectory.ToString() + "/error.aspx";

SendErrorEmail Method – Sends HTML Email with nicely styled exception stack trace

   

    protected void SendErrorEmail(String subject, String messageBody, Exception ex)
    {


        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("scheduledTasks@yourcompany.com");
        mail.To.Add("your@email.com");       
        mail.Subject = subject;
        mail.IsBodyHtml = true;


        string url = HttpContext.Current.Request.Url.AbsoluteUri;
        string referrer = (HttpContext.Current.Request.UrlReferrer == null) ? "" :
                HttpContext.Current.Request.UrlReferrer.AbsoluteUri;





        string formatting = @"
                            <style type='text/css'>
	                            body { font-family:Verdana; font-size:10pt; }
	                            .msg { font-family:Verdana; font-size:10pt; }
	                            .tbl, .tbl td { font-family:Verdana; font-size:10pt; }

	                            .tbl th { font-weight: bold; text-align: left; padding: 6px 9px 6px 9px; vertical-align: top; }
	                            .tbl td { 
		                            background-color: #f2f2f2; height: 18px; padding: 6px 9px 6px 9px; vertical-align: top;
	                            }
	                            .tbl td.lbl { font-weight: bold; }
	                            .tbl .alt td { background-color: #fff; }
	                            .stackTrace { font-size:8pt; background-color: #ccccc; padding: 10px; }
                            </style>
                            ";


        string preamble = @"
                            <p class='text'>An automated error has been sent at " + DateTime.Now.ToString("M/d/yyyy h:mm:ss tt") + @"</p>
                            <p>Page: <a href='" + url + @"'>" + url + @"</a></p>
                            <p>Referrer: <a href='" + referrer + @"'>" + referrer + @"</a></p>
                            ";

        // Add exception information if it exists
        if (ex != null)
        {

            preamble += @"
                        <p>
	                        <b>Exception Type:</b> " + ex.GetType().FullName + @"<br>
	                        <b>Exception Message:</b> " + ex.Message + @"
                        </p>";
            preamble += @"
                        <pre class='stackTrace'>Stack Trace" + ex.StackTrace + @"</pre>
                        ";

            if (ex.InnerException != null)
            {

                preamble += @"
                            <p>
	                            <b>Inner Exception Type:</b> " + ex.InnerException.GetType().FullName + @"<br>
	                            <b>Inner Exception Message:</b> " + ex.InnerException.Message + @"
                            </p>
                            <pre class='stackTrace'>Stack Trace" + ex.InnerException.StackTrace + @"</pre>
                            ";
            }
        }


        string body = @"
                        <html>
                        <head>
                        " + formatting + @"
                        </head>
                        <body>
                        <div class='msg'>
                        " + preamble + @"                                                                                        
                        <p>" + messageBody + @"</p>
                        </div>
                        </body>
                        </html>
                        ";


        mail.Body = body;

        new SmtpClient().Send(mail);
    }