Add ‘Tweet This’ button to your Asp.Net site. Twitter + Bit.ly url shortening

Adding your own ‘Tweet This’ buttons so your visitors can post to twitter (complete with URL shortening using Bit.ly) is really easy.

Just add a page ‘twitter.aspx’ to your site and remove all the HTML from the page.

Add this code:


Imports System.Net

Partial Public Class Twitter

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Request.QueryString("url") <> "" And Request.QueryString("com") <> "" Then

            TweetThis(Request.QueryString("url"), Request.QueryString("com"))

        End If

    End Sub

    Private Sub TweetThis(ByVal sIWTFURL As String, ByVal sComment As String)

        Dim sShortURL As String

        Dim sFullTweet As String

        sShortURL = ShortenURL(sIWTFURL)

        sFullTweet = sComment & " - " & sShortURL

        SentToTwitter(sFullTweet)

    End Sub

    Private Function ShortenURL(ByVal sURL As String) As String

        Dim sJSON As String

        Dim oWebClient As New WebClient

        Dim sBitlyURL As String

        Dim sShortURL As String

        sBitlyURL = "http://api.bit.ly/shorten?version=2.0.1&longUrl=" & Server.UrlEncode(sURL) & "&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07"

        sJSON = oWebClient.DownloadString(sBitlyURL)

        sJSON = Mid$(sJSON, InStr(sJSON, Chr(34) & "shortUrl" & Chr(34)) + 9)

        Do While sJSON.ToLower.StartsWith("http://") = False

            sJSON = Mid$(sJSON, 2)

        Loop

        sShortURL = sJSON.Split(Chr(34))(0)

        ShortenURL = sShortURL

    End Function

    Private Sub SentToTwitter(ByVal sComments As String)

        Dim sURL As String

        sURL = "http://twitter.com/home/?status=" & HttpUtility.UrlEncode(sComments)

        Response.Redirect(sURL, False)

    End Sub

End Class

Note: I am using the default Bit.ly login in the example, you should sign up for your own bit.ly account so you can track usage of your links.

Now you just need to add some javascript to your html pages:


<a href="#" onClick='javascript:PostToTwitter()'>Tweet This</a>

<script>
function PostToTwitter()
{
var sTweet = 'This is the default text that will appear in the tweet';
var ShareURL = window.location.href;

window.open('http://yoursite.com/twitter.aspx?url='+encodeURIComponent(ShareURL)+'&com='+encodeURIComponent(sTweet));
return false;
}
</script>

Convert www. to non www. urls for ASP.Net websites

Ok, redirecting your www. urls to non www. through IIS is a total pain. Using a url rewriter is a good option - but not always available in shared hosting. Here is a quick method if you have a small ASP.Net site or are using master pages.

Just add this code to the Page_Load sub in the master page, or to every page if you aren’t using master pages.


        If Not IsPostBack Then

            'redirect www. to non www.

            If LCase(Request.Url.AbsoluteUri).StartsWith("http://www.") = True Then

                Response.Status = "301 Moved Permanently"

                Dim sNewLocation As String

                sNewLocation = "http://" & Mid$(Request.Url.AbsoluteUri, 12)

                If LCase(sNewLocation).EndsWith("/default.aspx") Then

                    sNewLocation = Left$(sNewLocation, Len(sNewLocation) - 13)

                End If

                Response.AddHeader("Location", sNewLocation)

                Exit Sub

            End If

        End If

the code removes the www. and does a simple 301 redirect, while preserving case etc in the new url.

Come to think of it, now that Google can follow javascript redirects, you could add this code as a javascript sub to your pages. Anyone want to do a live test on their site? :)

MS Bing integrates Travel with their search engine

For Travel Meta Search engines, this doesn’t seem like good news…

Microsoft bought FareCast in April 09, and now it is helping power Bing Travel. I haven’t seen the travel link on the Australian version of Bing, but I am guessing that it won’t be long. Not that I really care what Bing does, but if the integrated travel works for Microsoft, then Google may do the same thing. And that would be news. How many travel sites depend on traffic from Google? What if that dried up?

Blog restoration, upgrade to 2.7 and other stuff

A little while ago I noticed that my blog was down, and that some of the files in the root directory had been corrupted. I contact my host (BlueFur) and they got back to me after 24 hours to say they had fixed the probem, and that all was good.

And they had, kind of.

They had restored the blog from a backup made over a year ago. Great going. That’ll teach me to rely on hosting backups, rather than do my own. Luckily I was able to get most of the posts from google cache, but all the comments made in the last 12 months were lost.

And I’ve finally rejoined society by upgrading to Wordpress 2.7 (up from 2.1). Finally, my posts are being saved automatically…

So now everything is in order. Time for some more posts!

Star Wars - By someone that hasn’t seen it

This is the best…



Star Wars: Retold (by someone who hasn’t seen it) from Joe Nicolosi on Vimeo.This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!Related posts:

  1. We Got Domain - over 10,000 aged domains for sale

SEOMoz Linkscape API wrapper in VB.Net

SEOMoz have just released a Free API to get Link info & their MozRank for domains. Before you can use the API, go to SEOMoz to get your AccessID and SecretKey.

The code here borrows from the function aws_GetSignature that comes with the Amazon Webservice.


        Private Function dcMozRankGet(ByVal sWebSiteURL As String) As Decimal
                Dim sAccessID As String = "YourAccessID"
                Dim sSecretKey As String = "YourSecretKey"
                Dim lExpires As Long = DateDiff("s", DateSerial(1970, 1, 1), Now()) + 300
                Dim sSafeSignature As String
                Dim sURLToFetch As String
                Dim sResult As String

                sWebSiteURL = HttpUtility.UrlEncode(sWebSiteURL)
                sSafeSignature = Encode(sAccessID, lExpires, sSecretKey, vbLf)

                sURLToFetch = "http://lsapi.seomoz.com/linkscape/mozrank/" & _
                sWebSiteURL & "?AccessID=" & sAccessID & "&Expires=" & lExpires & _
                "&Signature=" & sSafeSignature

                sResult = sGetData(sURLToFetch)
                sResult = Mid$(sResult, InStr(sResult, """umrp"":") + 7)
                sResult = sResult.Split(",")(0)
                dcMozRankGet = CDec(sResult)
        End Function

        Public Function Encode(ByVal sAccessID As String, _
        ByVal lExpires As Long, _
        ByVal SecretAccessKey As String, ByVal Separator As String) As String

                Dim sAccessExpires As String
                sAccessExpires = sAccessID & Separator & lExpires

                Dim strSig_UTF8 As Byte()
                Dim strSignature As String
                Dim objUTF8Encoder As UTF8Encoding
                Dim objHMACSHA1 As HMACSHA1

                objUTF8Encoder = New UTF8Encoding()
                strSig_UTF8 = objUTF8Encoder.GetBytes(sAccessExpires)

                objHMACSHA1 = New HMACSHA1( _
                     objUTF8Encoder.GetBytes(SecretAccessKey))
                strSignature = Convert.ToBase64String _
                     (objHMACSHA1.ComputeHash( _
                     objUTF8Encoder.GetBytes( _
                     sAccessExpires.ToCharArray())))

                Encode = HttpUtility.UrlEncode(strSignature)

        End Function

        Public Function sGetData(ByVal sURL As String, _
Optional ByRef oCookies As CookieContainer = Nothing) As String

                Dim Writer As StreamWriter = Nothing
                Dim WebRequestObject As HttpWebRequest
                Dim sr As StreamReader
                Dim WebResponseObject As HttpWebResponse
                Dim sbResultsBuilder As New StringBuilder
                Dim sBuffer(8192) As Char
                Dim iRetChars As Integer

                WebRequestObject = CType(WebRequest.Create(sURL), HttpWebRequest)
                WebRequestObject.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"
                WebRequestObject.Method = "GET"
                WebRequestObject.Timeout = 55000
                WebRequestObject.ReadWriteTimeout = 55000
                WebRequestObject.AllowAutoRedirect = True

                If Not (oCookies Is Nothing) Then
                        WebRequestObject.CookieContainer = oCookies
                End If

                WebResponseObject = CType(WebRequestObject.GetResponse(), HttpWebResponse)

                sr = New StreamReader(WebResponseObject.GetResponseStream)

                Do
                        iRetChars = sr.Read(sBuffer, 0, sBuffer.Length)
                        If iRetChars > 0 Then
                                sbResultsBuilder.Append(sBuffer, 0, iRetChars)
                        End If
                Loop While iRetChars > 0
                sGetData = sbResultsBuilder.ToString

        End Function

An example call is:

dcYourRank = dcMozRankGet("www.seomoz.org")

This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!

Related posts:

  1. Getting Flights info from the Kayak API in ASP.Net
  2. Allowing special characters (forward slash, hash, asterisk etc) in ASP.Net MVC URL parameters
  3. Automate FireFox with ASP.net

Copying the contents of one combo box to another

Quick code tip on how to copy the contents of a combo box (HTML Select) to another. Using JQuery, of course.


$(”#NewCombo”).html($(”#OriginalCombo”).html());

I use this for forms with several date pickers, country pickers etc.

This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!

Related posts:

  1. We Got Domain - over 10,000 aged domains for sale
  2. Cast your net wiiiide
  3. Copying cookies across domains in ASP.Net

Social Media sites to submit your programming content to

I have benefited hugely from submitting my blog posts to the various niche social media sites that focus on programming.

The best (for me) have been:

If you can get to front page on Digg, Reddit or Mixx then you’ve got more luck than me :)

This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!

Related posts:

  1. We Got Domain - over 10,000 aged domains for sale
  2. Hiding the Digg button from Diggers
  3. Cast your net wiiiide

Cool JQuery Plugins

JQuery - it really is all that & a packet of chips.

Smashing Magazine has just published 45+ New jQuery Techniques For Good User Experience and there is plenty of goodness in there.

For IWantThatHotel.com.au the JQuery plugins that I used were:

BGIFrame
Date Picker
jCarousel (but I am thinking of replacing it with this Content Slider
Lightbox

This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!

Related posts:

  1. We Got Domain - over 10,000 aged domains for sale
  2. Integrating JQuery with ASP.net - A Cool Client-side Alert Box

Getting ASP.Net MVC running on IIS6

I have recently finished a new site IWantThatHotel, my first in ASP.Net MVC.

Over the next couple of weeks I will be posting code examples in VB.Net MVC (of which there are hardly any, it seems).

But first, here is a link to 2 posts that were a huge help getting the clean MVC urls running on IIS6
Deploying ASP.NET MVC to IIS 6
Several options are presented - I chose “Use a wildcard mapping for aspnet_isapi.dll” (out of the 4 options presented) because I didn’t want to alter my routes or extensions, and URL rewriting is a pain.

There is a separate post which talks about improving the performance of this method, which is worth implementing:
Disabling wildcard mapping on subdirectories

This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!

No related posts.