Displaying Selected YouTube Data API Thumbnails On A Web Page Via ASP.NET Web Forms
Previously, I blogged about “Displaying Selected YouTube Video Thumbnails On An ASP.NET Web Forms Page,” when you know the video IDs of the thumbnails you want to hyperlink.
A reader recently asked me how to hyperlink YouTube video thumbnails based on searching for a keyword. I promised to address that, so here goes.
Interestingly enough, searching the YouTube Data API is accomplished in a REST-like manner quite similar to the methodology I used for shortening URLs in ASP.NET via the bit.ly API.
- Form a simple request URL to the YouTube Data API that contains the appropriate search parameters;
- Use a WebRequest to send that URL to Google, which returns an XML document with results;
- Use WebResponse to dump that stream into an XmlDocument;
- Use XPath and XmlNode‘s SelectNodes method to recursively get the thumbnails from each entry; and
- Bind up a pile of Hyperlink controls, which are added dynamically to a Panel control.
Sounds more complicated than it actually is. Let’s do it.
Continue reading: Displaying Selected YouTube Data API Thumbnails On A Web Page Via ASP.NET Web Forms »
Getting QueryString Values From A Rewritten URL / ASP.NET Routing URL
During today’s similcast of the ASP.NET Firestarter in Atlanta, G. Andrew Duthie discussed .NET 4′s new support for routing — or, what everyone in Web development calls “URL rewriting.” *
Someone online asked, “If I use routing, can I access query string variables using JavaScript?”
The question isn’t as confused as it sounds on the surface. Of course, if one uses routing / URL rewriting, it’s to remove query string variable and make them part of what appears to be a permanent file structure.
In other words, this:
http://www.server.com/path/to/file.aspx?v1=foo&v2=bar
Becomes this:
http://www.server.com/path/to/file/v1/foo/v2/bar/
The questioner really means, is there a way, after rewriting a URL, to extract key->value pairs from it via JavaScript? The answer is yes; rather than using the location.search property, which allows JavaScript to get the querystring parameters of a URL, we use location.pathname to get the part of the URL that follows the domain, and use that to create our key->value pairs.
Continue reading: Getting QueryString Values From A Rewritten URL / ASP.NET Routing URL »
MSDN Northeast Roadshow, Augusta, ME, May 11, 2010 Recap
After the nearly complete disappointment of Launch 2010 Boston, I had given serious thought to not going to the MSDN Northeast Roadshow stop in Augusta, ME on Tuesday, May 11. I figured it would be little more than a regurgitation of what happened at last month’s event.
I’m glad I decided to go. What a huge difference.
In half the time, Jim O’Neil and Chris Bowen provided immeasurably more valuable and interesting information about Visual Studio 2010 and changes to Silverlight, multithreading, ASP.NET and an overview of Windows Phone 7. Here’s a recap.

Jim O'Neil describes Silverlight 4's new video / webcam support features.
Silverlight 4: Lately, listening to Microsoft describe a new Web technology brings to mind the phrase “a day late and a dollar short.” That’s certainly the case with Silverlight 4.
Admittedly, Microsoft has shifted the emphasis behind Silverlight to be less a clone of Flash and more an extension to the Web of Windows Presentation Foundation. But the “new features” O’Neil described on Tuesday were very much old technologies for Flash, and pretty much obsolete tech given HTML5.
Specifically, Silverlight 4 supports Web cams and microphones; TCP/UDP; printing; and a multiple-trust-level model that includes access to the file system, cross-domain requests and COM integration. Additionally, the XAML one writes to render Silverlight, while still different from WPF XAML, is a lot more like WPF.
Silverlight is, therefore, weaker than Flash and stronger than it at the same time. But it seems mostly moot, given that Flash itself is falling out of favor in response to the HTML5 specification’s API support for audio, video and other complex objects. Admittedly, there are things one can do in Silverlight — namely, presenting stored data and integrating existing COM components, such as text-to-speech or an interface to a proprietary business object / program — that one cannot do directly in HTML5.
That seems to me more likely to matter when making a corporate intranet or the like. I didn’t see much hope for Silverlight when it was introduced in 2007, and I still don’t see a future for it.
Continue reading: MSDN Northeast Roadshow, Augusta, ME, May 11, 2010 Recap »
Shortening URLs With The bit.ly API Via ASP.NET
After yesterday’s post on using the Twitter API to post status updates on Twitter from an ASP.NET application, a follower of my client suggested using a URL shortening service to link to the articles headlines being posted as tweets.
That was an excellent idea of which I should have thought in the first place. And thanks to the extreme simplicity of the REST-like bit.ly API, shortening URLs is as simple as sending a request URL laced with querystring variables.
So here is a quick and simple ASP.NET subroutine, written in VB.NET, to shorten URLs with the bit.ly API:
Sub BitlyIt(ByVal strUser As String, ByVal strAPIKey As String, ByVal strLongUrl As String)
'This subroutine requires your page to have a label control named lblStatus
'build URL to shorten method resource
Dim strUri As New StringBuilder("http://api.bit.ly/shorten?")
strUri.Append("version=2.0.1")
'we want to get an XML response back from bit.ly, not the default JSON
strUri.Append("&format=xml")
strUri.Append("&longUrl=")
strUri.Append(Server.HtmlEncode(strLongUrl))
strUri.Append("&login=")
strUri.Append(Server.HTMLEncode(strUser))
strUri.Append("&apiKey=")
strUri.Append(Server.HTMLEncode(strAPIKey))
'create request for shorten resource
Dim objRequest As HttpWebRequest = WebRequest.Create(strUri.ToString())
'since we are passing querystring variables, our method is get
objRequest.Method = "GET"
'act as though we are sending a form
objRequest.ContentType = "application/x-www-form-urlencoded"
'don't wait for a 100 Continue HTTP response from bit.ly
objRequest.ServicePoint.Expect100Continue = False
'since we are using get, we need not send a request body; set content-length to 0
objRequest.ContentLength = 0
'we need to capture the XML being sent in response
'read the response into a new XML document
Dim objResponse As WebResponse = objRequest.GetResponse()
Dim objXML As New XmlDocument()
objXML.Load(objResponse.GetResponseStream())
'the response will have three node values we're primarily interested in
'the errorCode node will contain a numeric error code; 0 means success
Dim nErrorCode As XmlNode = objXML.SelectSingleNode("//errorCode")
'if there was an error, the errorMessage node will contain a user-friendly message useful for debugging
Dim nErrorMsg As XmlNode = objXML.SelectSingleNode("//errorMessage")
'if all went well, shortUrl will contain the full short URL created for our link
Dim nShortUrl As XmlNode = objXML.SelectSingleNode("//shortUrl")
'if we didn't get an errorCode value of 0, there was a problem; report the user-friendly message
'on success, report the short URL for our resource
If nErrorCode.InnerText <> "0" Then
lblStatus.Text = "Error returned. Code: " & nErrorCode.InnerText & "; Message: " & nErrorMsg.InnerText
Else
lblStatus.Text = nShortUrl.InnerText
End If
End Sub
Continue reading: Shortening URLs With The bit.ly API Via ASP.NET »
Posting Status Updates (Tweets) To A Twitter Profile Via ASP.NET
I have a client that posts several news stories to its Web site every day. So it makes tremendous sense for them to post the headlines to Twitter as tweets, so that followers might be informed of breaking news, or just what’s new on the site.
The Twitter API makes posting status updates (tweets) to Twitter as simple as invoking a WebRequest. Unfortunately, most of the ASP.NET examples on the Web that aim to show you how to post status updates are either written in C#, have coding errors, simply will not work, or all three.
So here’s a simple VB.NET subroutine that will post tweets.
Sub TwitIt(ByVal strUser As String, ByVal strPass As String, ByVal strMessage As String)
'this subroutine requires your ASP.NET page to have a label control with an ID of lblStatus
'create post variable for tweet
Dim strTweet As String = "status=" & Server.HtmlEncode(strMessage)
'convert post variable to byte array for transmission purposes
Dim bRequest As Byte() = System.Text.Encoding.ASCII.GetBytes(strTweet)
Try
'create HttpWebRequest to status update API resource
Dim objRequest As HttpWebRequest = WebRequest.Create("http://twitter.com/statuses/update.xml")
'pass basic authentication credentials
objRequest.Credentials = New NetworkCredential(strUser, strPass)
'set method to post and pass request as a form
objRequest.Method = "POST"
objRequest.ContentType = "application/x-www-form-urlencoded"
'tell the server it will not receive a 100 Continue HTTP response
objRequest.ServicePoint.Expect100Continue = False
'set content length of request
objRequest.ContentLength = bRequest.Length
'capture the stream (content) of the request
Dim objStream As Stream = objRequest.GetRequestStream()
'put the bytes into request
objStream.Write(bRequest, 0, bRequest.Length)
'close the stream to complete the request
objStream.Close()
'uncomment line below to report success
'lblStatus.Text = "Tweet sent!"
'You can also capture the XML response Twitter sends back
'uncomment lines below to capture responses
'Dim objResponse As WebResponse = objRequest.GetResponse()
'Dim objReader As New StreamReader(objResponse.GetResponseStream())
'lblStatus.Text = objReader.ReadToEnd()
Catch ex As Exception
'uncomment line below to report ASP.NET errors
'lblStatus.Text = ex.Message
End Try
End Sub
Continue reading: Posting Status Updates (Tweets) To A Twitter Profile Via ASP.NET »



