Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 520

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 535

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 542

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 578

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/gathadam/public_html/wp-settings.php on line 18

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

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

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.

Allowing special characters (forward slash, hash, asterisk etc) in ASP.Net MVC URL parameters

I’ve been getting into ASP.Net MVC a lot lately and there is plenty that is good about it.

One thing that is not good is the problems that MVC has when you have a special character (*,/,& etc) in your querystring.

Eg: Say you want to pass the param

url=http://google.com

MVC won’t like it - ‘/’ is a special character reserved as a separator for routes. But standard URL encoding it won’t work either:

url=http%3A%2F%2Fgoogle.com

MVC will still give a ‘HTTP Error 400 - Bad Request error’.

After looking around, it seems this error has been coming up a bit here and here and Phil Haack explains why standard encoding won’t work here.

One answer is to use base 64 encoding on any parameters that might contain the special characters.


        Public Function MyURLEncode(ByVal sInString As String) As String
                Dim sInStringNoSpaces As String = sInString.Replace(" ", "")
                Dim sURLEncoded As String = HttpUtility.UrlEncode(sInString)

                If sURLEncoded.Replace("+", "") = sInString.Replace(" ", "") Then
                        Return sURLEncoded
                Else
                        Return "=" & ToBase64(sInString)
                End If
        End Function

        Public Function MyURLDecode(ByVal sEncodedString As String) As String
                If sEncodedString.StartsWith("=") = True Then
                        MyURLDecode = FromBase64(Mid$(sEncodedString, 2))
                Else
                        MyURLDecode = sEncodedString.Replace("+", " ")
                End If
        End Function

        Private Function ToBase64(ByVal sInString As String) As String
                Dim btByteArray As Byte()
                Dim a As New System.Text.ASCIIEncoding()
                Dim sResult As String

                btByteArray = a.GetBytes(sInString)

                sResult = System.Convert.ToBase64String(btByteArray, 0, btByteArray.Length)
                sResult = sResult.Replace("+", "-").Replace("/", "_")
                ToBase64 = sResult
        End Function

        Private Function FromBase64(ByVal sBase64String As String) As String
                sBase64String = sBase64String.Replace("-", "+").Replace("_", "/")
                Dim obj As New ASCIIEncoding()
                FromBase64 = obj.GetString(Convert.FromBase64String(sBase64String))
        End Function

Because one of the useful things about MVC is the SEO-friendly URLs, the code doesn’t convert to Base 64 unless it is necessary.

To use these functions, here is a brief code snippet:


Public Class HotelsController
        Inherits System.Web.Mvc.Controller

        Function SelectHotelList(ByVal City As String, _
        ByVal Location As String)

                oSearch = New With {.City = MyURLEncode(City), .Location = MyURLEncode(Location)}
                Dim oDO As New RouteValueDictionary(oSearch)
                Return RedirectToAction("List", "Hotels", oDO)
        End Function

        Function List(ByVal City As String, _
                                    ByVal Location As String)

                City = MyURLDecode(City)
                Location = MyURLDecode(Location)

	...

        End Function
End Class

Update: As Justin pointed out below, ‘/’ is a valid character in the base64 alphabet. I have had a quick read about Modified Base64 for URL which basically replaces ‘+’ and ‘/’ characters with ‘-’ and ‘_’ (I leave the padding ‘=’, but you can remove them if you want). I have updated the code with these changes, however I can’t find a test case that puts ‘/’ into the base64 encoded string - anyone know of one?

Also, the above code only encodes ASCII values, if you want to encode UNICode strings, then swap ASCIIEncoding with UnicodeEncoding.

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

Related posts:

  1. SEOMoz Linkscape API wrapper in VB.Net
  2. Remove HTML tags and Javascript from pages
  3. Automate FireFox with ASP.net

Copying cookies across domains in ASP.Net

If you logging in to remote sites using HttpWebRequest then you are probably used to supplying a CookieContainer object to keep track of login sessions etc. However, sometimes a login will do a redirect, leaving your cookies referring to the login url and not usable on the next url.

Eg:
Post login details to
http://yourdomain/login
Site redirects (302) to:
http://yourdomain/admin

The login cookie is stuck on http://yourdomain/login and is not used at http://yourdomain/admin, so the login fails.

The key is to set:
WebRequestObject.AllowAutoRedirect = false
in the HttpWebRequest for http://yourdomain/login

Code to copy cookies:


Dim oOldCookies As New CookieContainer
Dim oNewCookies As New CookieContainer
Dim oOldCookiesCol As New CookieCollection

'Do HttpRequest on http://yourdomain/login, place cookies in oOldCookies
'Remember to set WebRequestObject.AllowAutoRedirect = false

oOldCookiesCollection = oOldCookies.GetCookies(New System.Uri("http://www.olddomain.com"))
For iC = 0 To oOldCookiesCol.Count - 1
     oNewCookies.Add(New Cookie(oOldCookiesCol(iC).Name, oOldCookiesCol(iC).Value, "", "http://www.newdomain.com"))
Next

'Now do HttpRequest on http://yourdomain/admin, using oNewCookies

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

Related posts:

  1. Easy Cross Domain Cookies (Sharing cookies between domains)
  2. How to set third-party cookies with iframe Facebook Applications
  3. Opening a new browser window with POST data
  4. SEOMoz Linkscape API wrapper in VB.Net