Archive

Posts Tagged ‘aspnet’

Using bit.ly URL Shortening From ASP.NET (REST/JSON)

May 7th, 2009 pj4533 No comments

As part of my ‘learning ASP.NET’ series of posts, I thought I would discuss using the bit.ly url shortening API. I will add auto shortening to my SpiniTweet application. SpiniTweet simply takes the last song played on WMFO from Spinitron, and creates a nicely formatted tweetable string. You click, ‘Tweet This’, and then you don’t have to re-type your tracks as you are djing on air to let people know what you are playing. I like to include a link to listen to ‘MFO live via the web, in case someone wants to tune in. This is the link I want to shorten.

First off, apparently bit.ly doesn’t reuse links, so in my case I really don’t need to do this programmatically. But where is the fun in that?!?! Plus, I want to eventually add the ability for any link to be shortened in SpiniTweet, not just the WMFO listener link.

After you sign up for bit.ly, you get your API key in your profile. This is needed because they limit the total number of connects at one time. Next, check out the documentation wiki! bit.ly uses a REST api. If you really want details you can go read that linked wiki page. The bit.ly REST api is simple. We will be most interested in the ’shorten’ command, but there are others for doing things like expanding links and getting stats.

Our goal will be to send a url similar to this:
Read more…

From Windows Forms to Web Forms: Enough The Same To Be Frustrating

May 1st, 2009 pj4533 No comments

In my previous entry I created an application using C# in .NET, using windows forms. While very basic, it performs its task well. Has a ‘tweet length’ that updates when text is entered, and uses a timer to update the text of the tweet to reflect the latest song being played from your Spinitron playlist. Unfortunately, the radio station’s computers use OpenSUSE, so even my target audience (while a small one), can’t really use it without bringing in a windows machine.

All the cool kids are always talking about ASP.NET so I figured I’d take a look. I have a background in C/C++. My day job has kept me busy in a very legacy codebase for the past 10+ years. I have broadened my skill set over the years to include things like Cocoa,MFC, and .NET (windows forms). I am no Vostokov but I can hold my own in a .DMP file, and Windbg is DEFINITELY my favorite debugger. Bottom line is, I feel prepared. However, I wouldn’t know a postback from a style sheet. But, how hard can it be? :)

I decided to start by just making a web application of SpiniTweet, the radio station twitter application from my previous post. This way people at the station could use it anywhere, and it would also give me a good intro to ASP.NET. One of the books I owned had a chapter on web forms, so I decided to start there.

Turns out, it seemed one of the principals in designing web forms was to make them similar to windows forms. In fact, I was delighted to find out I could reuse my windows form code! So I quickly just banged out a simple web application version.

Launched VS2008, and SpiniTweet4Web was born! Before I touched the code, I went into the designer and quickly made a similar form. It seemed clunky, after using web forms for so long, but I noticed that it was making the html page as I added controls, so I figured it felt that way cause it was following all the rules of how a web page is arranged. I got the basics done, leaving out the countdown timer, and the tweet length for now:

Ok, so its not pretty, but whatever, I am just learning, and I can deal with that later. After getting that arranged, I took a look in the code. It seemed to make sense. Page_Load() is the same as the Form1() constructor. So I just added my initialization code there. Double-clicking the button allowed me to have a _click() handler. Fantastic! It all makes sense!

First problem I ran across is understanding the postback functionality. For example, in Page_Load(), I called ‘UpdateFromSpinitron()’ which goes and grabs the latest song played on WMFO, and formats a nice string to be tweeted. If I modify the text and click ‘Tweet This’, you’d think it would just tweet whatever was in the text box, since I query the tweetMsg text box in the button handler here:

System.Net.ServicePointManager.Expect100Continue = false;HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(TwitterJsonUrl);

string post = string.Empty;using (TextWriter writer = new StringWriter()){   String tweetMsgStr = tweetMsg.Text;

   writer.Write("status={0}", HttpUtility.UrlEncode(tweetMsgStr));   post = writer.ToString();   Console.WriteLine("Post: {0}", post);}

Not so! Each time it does a postback, it calls Page_Load() again, which calls ‘UpdateFromSpinitron’ overwriting whatever I had changed in the textbox. This was the first, of many, similar but different experiences with ASP.NET. Further, this postback reinititalizing the page seems to be a common theme throughout various controls. Seems like a common issue. You get around it by only initializing if it isn’t a postback like so:

protected void Page_Load(object sender, EventArgs e){   if (!this.IsPostBack)   {         UpdateFromSpinitron();   }   AddCharCount();}

Easy enough. Now, how do I make a twitterish tweet length counter? This is where stuff started to get difficult. Getting the feedback on a per keyup seems to be very difficult on the server side. You can do ‘Autopostbacks’, which automatically send a postback anytime the control is updated, but only when you de-focus the control is it updated, and thus posted back to the server. Further, I came to the conclusion that you really don’t WANT it to be posting back after every keyup. So what else?

Enter AJAX. What the who? Oh yeah, I think I heard about that. Google, right? Talk about behind the curve. This is what living in a legacy codebase for 10 years will do to you.

So I picked up a couple of the biggest books on ASP.NET 3.5 I could find, both had chapters on AJAX. That taught me the basics of how the implementation works. But I wasn’t able to really get my length counter working until I found this webpage. It is a fantastic tutorial talking about exactly what I needed. Apparently you can’t get away from learning javascript!

So there you have it…my first attempt at ASP.NET. You can see the complete code for my website at codeplex. And even check it out running here: www.pj4533.com/spinitweet

Just a comment, I was hoping that ASP.NET would allow an identical workflow as working with windows forms. It does look like they are pushing in that direction, but they just aren’t quite there yet. This has been exciting to learn, and I plan to write many more web apps now! I also plan to add features to SpiniTweet such as a more sexy look (CSS pages), facebook integration, and URL shortening. I’ll post up some blog entries when I do so!

Categories: Techniques Tags: , , ,