Tip: jQuery AJAX calls to a WCF REST Service

06/11/2009

WCF 3.5 includes REST functionality and one of the features of the new WCF webHttp binding is to return results in a variety of ways that are URL accessible. WCF has always supported plain URL HTTP access, but it's not been real formal and had somewhat limited functionality as parameters had to be encodable as query string parameters. With the webHttp binding there's now an official WCF protocol geared towards providing ASP.NET AJAX JSON compatibility (using WebScript behavior) as well of a slightly cleaner raw JSON implementation (basic webHttp binding).

You can return XML (default), JSON or raw data from WCF REST services. Regardless of content type, natively WCF always wants to return content in a 'wrapped' format which means that both inbound parameters and outbound results are wrapped into an object.

Let's take a look at the message format for a  REST JSON service method.

[ServiceContract(Name="StockService",Namespace="JsonStockService")]    
public interface IJsonStockService
{
    [OperationContract]          
    [WebInvoke(Method="POST",
               BodyStyle=WebMessageBodyStyle.Wrapped,
               ResponseFormat=WebMessageFormat.Json
    )]
    StockQuote GetStockQuote(string symbol);

..

The input message on the wire looks like this:

{"symbol":"MSFT"}

The response looks like this:

{"GetStockQuoteResult":
        {"Company":"MICROSOFT CP",
        "LastPrice":30.00,
        "LastQuoteTime":
        "\/Date(1208559600000-0700)\/",
        "LastQuoteTimeString":"Apr 18, 4:00PM",
        "NetChange":0.78,
        "OpenPrice":29.99,
        "Symbol":"MSFT"}
}

Notice that in both cases an object is used. For the inbound data all parameters are wrapped into an object and rather than just passing the value, the name of the parameter becomes a property in the JSON object map that gets sent to the server. This is actually quite useful - if you're just sending a raw JSON structure you could only pass a single parameter to the server - and that option is also available via the Web BodyStyle=WebMessageBodyStyle.Bare option on the service method.

The outbound result set is also wrapped into an object which is a lot less useful. This is a hold over from WCF which wraps all responses into a message result object, which usually makes sense in order to support multiple result values (ie. out parameters etc.). In a Web scenario however this doesn't really buy you much. Nevertheless if you want to pass multiple parameters to the server you have to use this wrapped format along with the result value.

Calling with jQuery

If you're using jQuery and you'd like to call a WCF REST service it's actually quite easy either with bare or wrapped messages. Bare messages are easier to work with since they skip the wrapping shown above, but as I mentioned you're limited to a single input parameter. So if your service has any complexity you'll likely want to use wrapped messages.

You can opt to either call services using the ASP.NET Ajax logic (WebScriptService behavior) or using the raw service functionality which is shown above.

To call these methods with jQuery is fairly straight forward in concept - jQuery includes both low level and highlevel methods that can call a URL and return JSON data. The two methods available are $.getJSON() which automatically parses result JSON data and $.ajax(), which is a lower level function that has many options for making remote calls and returning data.

getJSON() is useful for simple scenarios where the server returns JSON, but it doesn't allow you to pass JSON data TO the server. The only way to send data to the server with getJSON is via query string or POST data that is sent as standard POST key/value pairs. In all but the simplest scenarios getJSON() is not all that useful.

The lower level $.ajax method is more flexible, but even so it still lacks the capability to pass JSON data TO the server. So little extra work and some external JSON support is required to create JSON output on the client as well as dealing with Microsoft Ajax's date formatting.

Posted in: C# and .NET| Tags: Format Tip WCF ajax jQuery REST url webhttp json functionality content

6 Tips for Google Webmaster Tools

05/19/2009

Google Webmaster Tools is a free service that provides a wealth of information directly from Google. Once you have verified a site with Google, they’ll give you access to all sorts of information.

Here are just a few features of Google Webmaster Tools:

1. Errors
Google Webmaster Tools will show all sorts of errors with a site. Not only does it show broken links on the site, but also links that are driving traffic to the site for which there is no valid page. Google even tells you pages it knows about but has been restricted from crawling. That’s good to know incase someone accidently blocks to much.
Google Webmaster Tools Error Report

Google Webmaster Tools Error Report

2. Set Site Defaults
Tell Google to show your page with the www or without, set a geographic target and select if you want the images show up in Google’s enhanced image search; aka Google Image Labeler.
Google Webmaster Tools Site Defaults

Google Webmaster Tools Site Defaults

3. Analyze Meta Descriptions and Title Tags
Google will provide a list of URLs that have duplicate title tags or duplicate meta descriptions as well as if there are pages with to short, or to long, meta descriptions or titles.
Google Webmaster Tools Analyzing Meta Data

Google Webmaster Tools Analyzing Meta Data

4. Top Search Queries
Ever wondered what people search for that your show up for? Outside of the obvious of course. This type of information is available in Google Webmaster Tools. It shows what a site is showing up for and what people are clicking though on along with your ranking. It can even be filtered by type of search (web, image, mobile) and by country.
Google Webmaster Tools Top Search Queries

Google Webmaster Tools Top Search Queries

5. Manage Sitelinks
If a site is lucky enough to get an additional block of links under their listing in Google, these can be managed in Google Webmaster Tools. You can’t tell Google what pages to add, but you can tell Google not to show a sitelink it has created.
Google Sitelinks

Google Sitelinks

6. Enhance 404 Error Pages
Google can provide a bit of JavaScript that can be embed on a 404 error page so that when it loads, Google will try to guess what the user was looking for based on what Google has indexed. It also comes with a handy Google site search box.
Google Webmaster Tools Enhanced 404 Error Pages

Google Webmaster Tools Enhanced 404 Error Pages

These are just a few of the features of Google Webmaster Tools. Google continues to add additional features and functionality and we can only hope other search engines follow suit.

Posted in: SEO-Webmaster| Tags: SEO Webmaster Internet Tip Google Google Webmaster Tools

Hire a webmaster? Follow these rules:

05/19/2009

A common frustration they tell me is they don’t have a handle on their website in part because they have a difficult time getting in touch with their webmaster, and have no idea how to update their sites on their own. Does this sound familiar to you?
First, make sure you have the keys to your website. You have to be in complete control of your website, even if you hire someone to do the design and upkeep.

This means you have to be the one that registers the domain name and establishes a hosting account.

Why?

Because that way you can fire your webmaster and hire somebody different without the pain and frustration of trying to pry your website files, usernames and passwords out of the fingers of the person you just fired. Transferring a website is not overly difficult, but there are typically downtimes and room for errors that you shouldn’t have to deal with.

I suggest that you register your domain name at my service, which is a private label of GoDaddy. But you should host your website somewhere else for cost/security/peace-of-mind reasons we don’t need to get into here.

I recommend Kiosk hosting for script-intensive websites, and Hostgator for basic blog and brochure type sites or mini-sites (be sure to snag their coupon code on the homepage). The simplest plan of either service is fine to start with, and scale up as your needs grow. If you need visual help in doing this step, check out my free First Website Tutorial site.

Second, you should get an understanding of the basics of website management. This means learning how to upload files, how to go in and make simple changes, and how to set up certain features of a website (like 404 error pages, email accounts, or simple file & folder name redirects).

You can learn how to do this in a couple hours from the videos Chris Morris and I made at DiscovercPanel.com for less money than 1 hour of your webmaster’s design and update time. You’ll be amazed at how simple these website management skills are, and how much you will likely be overcharged for them by a webmaster!

Third, you should make your first website a blog. Even if you don’t plan on doing any real ongoing updates, using the WordPress platform for your website makesit even easier to update the way your website looks without going to Geek School.

Personally, I prefer to use the blogging service Blogi360 to run my blog (think WordPress on steroids) because of it’s publishing power for extra traffic, tech support team already in place, and they fiddle with all the plugins and theme installs so I don’t have to. Whether you use basic WordPress or Blogi360, though, if you can use yourmouse and keyboard, you can update your website pretty easily. (See the free audio training at UnstoppableBlogging.com for strategies in this area).

Fourth, if you do hire a webmaster and/or designer, don’t hire someone solely based on cost. Check out their portfolio, communicate with their references, and be very specific about what your needs are and your timeline for completion. Remember, the phrase “You get what you pay for” is typically true when it comes to webmasters, although it’s also easy for you to be overcharged if you don’t have a clue about what you’re asking about.

Ultimately, you should spend your time doing what you do best - that’s where the real money is made in any business. But by knowing a few of the basics of website management, you can avoid being held hostage and feel confident that your website will be working for you and not against you.

Posted in: SEO-Webmaster| Tags: Webmaster Website Hire Tip time part service name frustration domain touch

Hot Posts

Latest posts

Tags

Others

Sponsors

asp.net interview questions