More Really Useful ASP.NET C# Functions

I'm not taking credit for all these, some I found and maybe adapted over the past few months but have found them invaluable - So thought I would pass them on for everyone else to use.

Convert BBCode Function

public string BBCode(string strTextToReplace) { ////Define regex Regex regExp; ////Regex for URL tag without anchor regExp = new Regex(@"\[url\]([^\]]+)\[\/url\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$1</a>"); ////Regex for URL with anchor regExp = new Regex(@"\[url=([^\]]+)\]([^\]]+)\[\/url\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$2</a>"); ////Image regex regExp = new Regex(@"\[img\]([^\]]+)\[\/img\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<img src=\"$1\" />"); ////Bold text regExp = new Regex(@"\[b\](.+?)\[\/b\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<b>$1</b>"); ////Italic text regExp = new Regex(@"\[i\](.+?)\[\/i\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<i>$1</i>"); ////Underline text regExp = new Regex(@"\[u\](.+?)\[\/u\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<u>$1</u>"); ////Font size regExp = new Regex(@"\[size=([^\]]+)\]([^\]]+)\[\/size\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"font-size: $1px\">$2</span>"); ////Font color regExp = new Regex(@"\[color=([^\]]+)\]([^\]]+)\[\/color\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"color: $1\">$2</span>"); return strTextToReplace; }

Strip HTML From String Function

public string StripHTMLFromString(string htmlText) { return Regex.Replace(htmlText, "<[^>]*>", string.Empty); }

Count Words In A String Function

public int WordCount(string Text) { string tmpStr; tmpStr = Text.Replace("\t", " ").Trim(); tmpStr = tmpStr.Replace("\n", " "); tmpStr = tmpStr.Replace("\r", " "); while (tmpStr.IndexOf(" ") != -1) tmpStr = tmpStr.Replace(" ", " "); return tmpStr.Split(' ').Length; }

List Of Free / Open Source ASP.NET Web Applications

As a big ASP.NET advocate, I'm loving the recent surge in open source / free ASP.NET applications that are hitting the web - And what's more because Microsoft have given us some great building foundations with the frameworks like the ASP.NET Membership Provider, a lot of these open source programs are very high quality!  More so than some of the paid applications.

I thought it would be a good idea to create an ever growing list of all the open source ASP.NET applications I could find - Obviously I haven't had chance to download or install every application so I can't vouch for them (The only ones I have used I'll mark with a *)

e-Commerce / Online Shopping Carts

  1. Dash Commerce - http://dashcommerce.org
  2. Dot Shopping Cart - http://www.dotshoppingcart.com/
  3. VevoCart - http://www.vevocart.com/
  4. e-Shop ASP.NET - http://fkn1337.com/e-shop-aspnet-mysql-script/
  5. PressTopia Shop - http://presstopia.com/dnn/Default.aspx?tabid=236 More...

(ASPLD) ASP.NET Link Directory

Well I have finally done it...  I managed to finish the first version of ASPLD and have decided to open it up to the ASP.NET open source scene.  I was going to charge few bucks initially but just decided it would be much better all round if I just let it be open source, in the hope people might submit a few themes and if it becomes fairly popular I'll really give version 2 a lot of time.

This version 1 is pretty scaleable and you can customise it fairly easily, being fairly new to ASP.NET (Been doing it about 12 months) I hope to really make the code more efficient over the next versions as I gain more experience, and maybe even change to MVC once its released.. But we'll see! More...

BlogEngine.NET & Windows Live Writer

I have had my blog for some time now, I built it completely from scratch while I was learning ASP.NET 2.0..  I thought it would be a great thing to do to help me learn, and also if its good enough I could even offer it as a blogging platform. 

Well now I look at it and realise how much of a shit job I did!  Ok its not that bad, it functioned well and was pretty quick but just lacked all the good features and was hard to update as I had built the entire thing using SQLDataReader and hard coded a lot of things.

Recently while browsing through DotNetKicks I found a post on a blog called Janko, and he was talking about BlogEngine.NET and how good it was.  I have been meaning to change for some time, and tried to have a go with SubText but didn't get very far!  So after reading Janko's post and asking a few questions I decided to give it a go!  It was easy to install and I have slightly adapted one of the original Themes (As you can see). I posted another question at Janko's about code formatting and got a reply about using Windows Live Writer..More...

Moving From VB To C#

Well I recently took the plunge and thought I would swallow my pride and learn C# (I told myself at Christmas I'd stay true to ASP.NET VB!), and think about changing to it properly.

As I am fairly new to ASP.NET as well, I thought this was going to be a total nightmare...  So I thought balls out, lets just make an application!  I opted for a Link Directory, as I know there is a popular PHP one called phpld but there isn't really a true ASP.NET Link directory. The ones that are out there currently are a bit of a mess from a coding point of view.. What I mean is they are more ASP than .NET, they use inc files and don't use any code behinds or classes etc...  So I thought this would be a good chance to learn C# and create a well built Link Directory that hopefully people will use.

Well after a week with a few headaches, I managed to get the beta version up

http://directory.n3o.co.uk More...

Easy ASP.NET UK Date Formatting

When I first started coding in ASP.NET for some really stupid reason my nemesis was always formatting dates, trying to get them to to display how I wanted them to and also in a UK format (Day first, Month then Year).

After some hunting I found out how easy it actually is using the built in .ToString() method...  Below is an example function that will return any date in the UK format

Function: Just pass in a date (You can use now() or whatever you like) More...

Create A Dynamic ASP.NET 2.0 SEO Sitemap

The sitemaps protocol is a great and universal way to tell the search engines about all you're web pages - If (like me) you develop alot of dynamic websites that are driven off large SQL databases, trying to make sure all you're pages get crawled/found is really important... This really easy sitemap is a must in you're web design toolkit.

In this tutorial I am going to grab all the products from a database and just loop through them (Using a SQLDataReader and then using the new XmlTextWriter), you can use the same logic for pretty much every website - I have used this script on about a dozen websites and just changd the SQL to the appropriate table names and field names etc..

Firstly you want to make sure you have a robots.txt file in the root of you're website, and add the following to it

User-agent: *
Sitemap: http://www.YOUR-DOMAIN-NAME.co.uk/sitemap.aspx

Obviously if you already have a robots.txt then just add the bolded line and change the domain name to you're website domain - This tutorial will create the sitemap.aspx file for you. More...