Home Contact RSS

Archive for November, 2008

Team System Web Access Language Packs Are Ready!

Community made a great job this month. Microsoft MVPs have localized Team System Web Access (TSWA) into 7 new languages. With support of Microsoft Turkey, Hakan Eskici (TSWA Program Manager), Tufan Erdinc, we have built the Turkish language pack. The full list of language packs are below.

To download any of the language packs that you are interested in, visit Team System Web Access Localization Project on CodePlex.

How to generate RSS feeds without any 3rd party components

RSS feeds are widely used in today’s internet world. Thus we, developers, have to implement it on our websites and projects most of the time. There are many free and paid components that generates RSS feeds but in fact you may generate it by yourself as well.

I will post a generic handler’s source code to show you how to do that.

Post is a class that has some basic properties like Url, Title, Teaser, etc. You may need to write your own class and implement these properties in a basic manner. Then you will need to create a GetPosts() method that returns a list of Post objects.

Read the rest of this entry »

Easiest way of global error handling in an ASP.NET application

Since there are different ways of global error handling in ASP.NET applications and you have to implement the one that best fits into your project’s architecture, I will not be discussing all the possibilities that ASP.NET provides you. However, I will just share a small code snippet that you can use to catch unhandled exceptions and write a small piece of code to send you an email whenever an error occurs. Just put the following code in your project’s Global.asax file and it is supposed to work in case you have your SMTP server settings set correctly in the web.config file or you should modify the email sending part depending on your SMTP configuration. Hope it is going to be useful for you.

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();

    if (ex != null)
    {
        if (ex.InnerException != null)
        {
            ex = ex.InnerException;
        }

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine("EXCEPTION - BEGIN<br/><br/>");
        sb.AppendFormat("URL: {0}<br/>", Request.Url);
        sb.AppendFormat("Time: {0}<br/><br/>", DateTime.Now);
        sb.AppendLine("EXCEPTION DETAILS:<br/><br/>");
        sb.Append(ex);
        sb.Append("<br/><br/>EXCEPTION - END");
        sb.Replace("\n", "<br/>");

        try
        {
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

            mail.To.Add("yourname@yourdomain.com");
            mail.Body = sb.ToString();
            mail.BodyEncoding = Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Subject = "Exception occured at sunali.com";
            mail.SubjectEncoding = Encoding.UTF8;

            smtp.Send(mail);
        }
        catch { }
    }
}

Web Image Maker control by Tom Crane

Web Image Maker is an open source and pretty useful Image Maker control by Tom Crane. Feel free to give it a try as I can confirm that it works pretty fine.

Read more and download the code at http://www.theguildnetwork.com/tgn/articles/imageControl_page1.aspx

Strip all HTML tags from string

This is something we need very often. Either for security reasons or just because of some business rules. I will share a small code snippet with you as an extension method to string type so you can take advantages of using Regular Expressions to remove HTML tags from any user input.

public static string StripHtml(this string value)
{
    if (string.IsNullOrEmpty(value))
    {
        return string.Empty;
    }

    value = Regex.Replace(value, @"<(.|\n)*?>", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);

    return value;
}

And this is how you need to use it:

string var = txUserInput.Text;
var = var.StringHtml();

BuildManager.CreateInstanceFromVirtualPath

BuildManager is a class located in System.Web.Compilation namespace and contains a method named CreateInstanceFromVirtualPath. Using this static method, you may create instances of your UserControl, Page, Generic Handler or those kind of stuff. This might be useful in case you need to create some controls on the fly, etc.

The following piece of code shows how to use it.

UserControl instance = (UserControl)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath("~/Controls/Somefile.ascx", typeof(UserControl));

How to uppercase the first letter of a word or sentence

When I used to use PHP to develop my web applications, I really loved its basic but useful method named ucfirst. This function is explained using a simple explanation on PHP documentation:

ucfirstMake a string’s first character uppercase

I believe within all those useful and complex libraries taking place in .NET Framework, it still misses these kind of basic and useful methods. However, the good side is that we can extend it using our custom extension methods.

Because I needed to implement such a feature as PHP offers using ucfirst function, I wrote the following simple extension to achieve the same functionality.

/// <summary>
/// Capitalizes the first letter of the word.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ToUpperFirst(this string value)
{
    if (string.IsNullOrEmpty(value))
    {
        return string.Empty;
    }

    if (value.Length > 1)
    {
        return
            string.Format("{0}{1}",
                value.Substring(0, 1).ToUpper(),
                value.Substring(1).ToLower()
            );
    }
    else
    {
        return value.ToUpper();
    }
}

And this is how to use it:

string val = "something";
val = val.ToUpperFirst(); // result = Something