Thursday, April 12, 2012

Northwind-mania: Exposing restful data using Web API

I will be demonstrating a series of technologies using the well known Nothwind database. I regularly use this sample database from Microsoft for demos because of its simplicity. I am calling the blog series “Northwind-mania”.

My first post is on Web API.

MVC 4.0 provides a new template that exposes restful data through a new feature named Web API. I am using Visual Studio 2010 for this article. Follow these steps to create a simple application based on the Northwind database and Web API.

Pre-requisites:

  • Visual Studio 2010
  • Northwind database

1. Download and install ASP.NET MVC 4.0.

2. In Visual Studio 2010, Click on File >> New Project. Choose the “ASP.NET MVC 4 Web Application” template.

image

3. On the next window, choose the “Web API” project template.

image

4. The next step is to add the Northwind data model. Right click on the Models folder then select Add >> New Item.

image

5. Select “Data” in the left pane, click on “ADO.NET Entity Data Model”. Name the model “NorthwindModel.edmx”. Finally, click on the “Add” button.

image

6. On the “Entity Data Model Wizard”, select “Generate from database” then click on Next.

image

7. Configure a connection to your Northwind database on the next window then click Next.

image

8. Select all tables by clicking on the checkbox beside Tables, then click Finish.

image

9. Hit “CTRL SHIFT B” in order to compile the application.

10. Delete the ValuesController.cs file in the Controllers folder as we will not need this file.

image

11. Right click on the Controllers folder then select Add >> New Item… >> Web API Controller Class.

image

12. Since we will be exposing the data in the Categories table using Web API, name the controller CategoryController then click on the Add button.

image

13. Replace the methods in CategoryController.cs with the following code in the CategoryController class:

NorthwindEntities ctx = new NorthwindEntities();
public IEnumerable<Category> GetAllCategory()
{
   return ctx.Categories.ToList();
}

public Category GetCategoryById(int id)
{
   var category = ctx.Categories.FirstOrDefault((c) => c.CategoryID == id);

   if (category == null)
   {
       var resp = new HttpResponseMessage(System.Net.HttpStatusCode.NotFound);
       throw new HttpResponseException(resp);
   }
   return category;
}

public IEnumerable<Category> GetCategoryByName(string name)
{
   return ctx.Categories
       .Where(c => c.CategoryName.Contains(name))
       .Select(c => c);
}

14. Click on F5 to run your application.

image

15. In order to access the restful Category data, add “api/category” to the address URL. If you are using IE, you may see the following dialog at the bottom of your browser. This is because JSON data is being send from your server application to the browser.

image

If the above happens, the easiest work-around is to copy the URL into another browser like Chrome. This should result in all categories being displayed as shown below:

image

The above was rendered using the following method in the controller:

public IEnumerable<Category> GetAllCategory() {
   return ctx.Categories.ToList();
}

To view category with id=7, simply add /7 to the URL:

image

This was rendered with the following controller method:

public Category GetCategoryById(int id) {
   var category = ctx.Categories.FirstOrDefault((c) => c.CategoryID == id);

   if (category == null) {
       var resp = new HttpResponseMessage(System.Net.HttpStatusCode.NotFound);
       throw new HttpResponseException(resp);
   }
   return category;
}

To view category with CategoryName of Produce, replace “7” with “?name=produce”:

image

Finally, the above was rendered by the following method:

public IEnumerable<Category> GetCategoryByName(string name)
{
   return ctx.Categories
       .Where(c => c.CategoryName.Contains(name))
       .Select(c => c);
}

Friday, March 30, 2012

"Failed to connect to device as it is pin locked" error

I experienced the error "Failed to connect to device as it is pin locked." while deploying a Windows Phone 7 application onto my Samsung Focus phone. The solution was:

1) Login to your apphub account at http://create.microsoft.com/
2) Click on your profile name in the top right-hand corner of the screen
3) On the page that displays next, click on the "devices" link.
4) Delete the device that you are not able to deploy on. In my case, I just had one device.
5) Close the AppHup page
6) Start Zune
7) Register the phone using the following application: Start >> All Programs >> Windows Phone SDK 7.1 >> Windows Phone Developer Registration
8) Go back into Visual Studio and deploy your application into the phone device.

Monday, March 12, 2012

TechFest 2012 on Saturday Apr 28, 2012 with Scott Guthrie


Date & Time: Saturday April 28, 2012 8:00 AM to 5:30 PM 
Location: BCIT Burnaby Campus, 3700 Willingdon Ave, Burnaby, BC, V5G 3H2
Food: Snacks, Coffee, and Lunch
Wireless Internet Access: FREE
Closest skytrain stations: Brentwood Mall or Metrotown

Topic: TechFest 2012 on Saturday Apr 28, 2012 with Scott Guthrie

In case you have not heard yet, registrations have started for the mother of all Vancouver technology festivals. We call it TechFest 2012 and it is happening on Saturday April 28, 2012. Our keynote speaker is none other than Scott Guthrie himself. Check out Scott's wikipedia page. This is a once-in-a-lifetime opportunity to see Scott Guthrie in person.

There will be six tracks:

- Azure (Sponsored by Microsoft)
- CMS (Sponsored by DotNetNuke)
- OS
- Mobile (Sponsored by Microsoft)
- Development
- Database

You can view the detailed sessions at http://www.vancouvertechfest.com/Sessions.aspx.

Registration:

Early bird special $50 valid until March 31, 2012. Register now.

After March 31, 2012: $75

Sunday, March 11, 2012

Installation of Windows 8 Consumer Preview on Oracle's VirtualBox 4.1.8 for Windows

Below are a list of VM technologies that currently support Windows 8:

- Hyper-V in Windows 8 Developer Preview
- Hyper-V in Windows Server 2008 R2
- VMware Workstation 8.0.2 for Windows
- Oracle's VirtualBox 4.1.8 for Windows
- Parallels Workstation 6 for Windows
- Parallels Desktop 4 for Windows
- XenDesktop 5.5

My personal experience with Oracle's VirtualBox version 4.1.8 was smooth. I was able to install both "Windows 8" and "Windows 8 Server" without any problems.

Here are the steps I took for installing Windows 8 (Consumer Preview) on Oracle's VirtualBox 4.1.8 for Windows:

1) Download Orcale's Virtual Box host application from: https://www.virtualbox.org/wiki/Downloads. After you download the product, go ahead and install it.

2) Download the Windows Server 8 (Consumer Preview) 64-bit ISO image file from: http://windows.microsoft.com/en-US/windows-8/iso.

3) Create a directory named Win8ServerCP (or any other name you fancy) somewhere on your hard drive.
Remember this location as this is where we will later store the VM.

4) Start Oracle's VirtualBox

5) On the left side of the toolbar, click on the "New" button.

6) Click "Next" on the "Welcome to the New Virtual Machine Wizard!" dialog.

7) On the next "VM Name and OS Type" dialog, enter/choose the following:

Name:    Windows 8 64-bit Consumer Preview
Operating System:  Microsoft Windows
Version:   Windows 8 (64 bit)

Click "Next"

8) Click Next on the "Memory" dialog.

9) Click Next on the "Virtual Hard Disk" dialog.

10) On the "Welcome to the Virtual disk creation wizard" dialog accept the default file type "VDI (Virtual Disk Image)" and click Next.

11) Click Next on the "Virtual disk storage details" dialog.

12) On the "Virtual disk file location and size" dialog, click on the browse folder icon and navigate to the folder named Win8ServerCP created in step 1 above, then click Save.

Click Next to move on to the next dialog.

13) Click Create on the "Summary" window. It will take a while for the VM file to get created. Be patient.

14) In the left pane, double click on the virtual machine named "Windows 8 64-bit Consumer Preview".

15) Click Next on the "Welcome to the First Run Wizard!" window.

16) On the "Select Installation Media" window, click on the browse folder icon and select the downloaded ISO image file then click Next.

17) Click Start on the "Summary" window.

18) Complete the self-explanatory installation process of the operating system.

Thursday, March 1, 2012

Some Windows 8 Keyboard Shortcuts

I downloaded the Windows 8 Consumer Preview today, a day after it was released. I succeeded in getting it installed on an Oracle VirtualBox version 4.1.8. It was a fairly painless exercise.

It is quite a challenge getting used to the absence of the start button. I dug up the following shortcuts that make it easier for me to navigate this new O/S:

Peek into Desktop >> Win + ,
Start Narrator >> Win + Enter
List of Narrator Commands >> Win + ALT + F1
Stop Narrator >> Win + ALT + ESC
All Charms >> Win + C
Search Charm >> Win + Q
Share Charm >> Win + H
Start Charm >> Win
Device Charm >> Win + K
Settings Charm >> Win + I
Multiple Display Options >> Win + P
Search Apps >> Win + Q
Windows Explorer on Desktop >> Win + E
Search Settings >> Win + W
Search Files >> Win + F
Show Desktop >> Win + D
Start Button Functionality >> Win + X
Close an app >> Win + F4
Cycle through notifications >> Win + V
Cycle through applications >> Win + Tab
Toggle AppBar >> Win + Z
Swap from a snapped app to full screen >> Win + J

I'll update this list as I learn more. Meantime, enjoy !!!

March 17, 2012 - Found this great article on "Windows 8 How To": http://blogs.msdn.com/b/alfredth/archive/2012/03/10/dr-z-s-windows-8-how-tos.aspx

Thursday, February 23, 2012

How do you install a SharePoint 2010 Feature that appears to be missing.

Similar to a previous post, I got the following error while creating the "WIKI Publishing" site collection is SharePoint 2010:

Dependency feature with id 14aafd3a-fcb9-4bb7-9ad7-d8e36b663bbd for feature 'BaseSite' (id: b21b090c-c796-4b0f-ac0f-7ef1659c20ae) is not installed

I determined that 14aafd3a-fcb9-4bb7-9ad7-d8e36b663bbd refers to a feature named "LocalSiteDirectoryControl" from this site:

http://blogs.msdn.com/b/mcsnoiwb/archive/2010/01/07/features-and-their-guid-s-in-sp2010.aspx

I then executed the following command to activate the feature.

INSTALL-SPFEATURE -Path "LocalSiteDirectoryControl"

Thereafter, it was smooth sailing.

Wednesday, February 1, 2012

How do you find out the name of a feature from the GUID in SharePoint 2010?

While trying to import an exported site onto a different SharePoint instance, I encountered the following error:

The site template requires that the Feature {d57f4817-f1f9-42aa-863c-139804c731b0} be installed in the farm or site collection.
Troubleshoot issues with Microsoft SharePoint Foundation.

Correlation ID: 59577f16-fffc-422c-8b16-458cfb62adaf

I needed to figure out what feature was missing on the destination SharePoint 2010 instance. The following Powershell command came in handy:

PS C:\> GET-SPFeature -Identity "d57f4817-f1f9-42aa-863c-139804c731b0"

DisplayName                    Id                                       Scope
-----------                    --                                       -----
FBAManagement                  d57f4817-f1f9-42aa-863c-139804c731b0     Site


It turned out that I had installed a codeplex "Forms Based Authentication Management" feature on the source site that facilitates managing forms-based authentication users with the aspnetdb.mdf database.