Monday, April 15, 2013

REST Web Services With ASP.Net Web API

This blog post is first of the Connecting client side scripts to SQL Server. It will form the basis for the rest the blog posts in this series.
Web services are used to connect from Flex to to SQL Server. Web services in general can be categorized into SOAP (Simple Object Access Protocol) and REST (Representational State Transfer). This post will be using .Net based REST web services, which have lot less over overhead and provides the data as XML/JSON to the client.


The following software is used for what is described in the post:
  • Visual Studio 2012 (Professional or Visual Studio Express 2012 for Web)
  • SQL Server 2012 (Full or Express or Express Local DB)
  • IIS (Full or Express)
  • ASP.NET Web API (Installed as part of MVC4 during Visual Studio Installation).
To install all these components it is recommended to Microsoft Web Platform Installer (Web PI). This project can even be done using Visual Studio 2010, but in that case ASP.NET MVC 4 needs to be installed separately in using Web PI.

  • Open Visual Studio 2012. Click New Project and Select Web under Visual C#. Using .Net Framework 4.5, select ASP.NET MVC 4 Web Application. Name the project as "DataService".
    New Project
  • Select Web API as Project Template and Razor as View engine.
    MVC Project Type
  • This step only applies if you are using SQL Server Express LocalDB. In case you are using Express or Standard editions, SQL Server Management Studio can be used to manage databases. Open Web.config. In ConnectionStrings section, go to DefautConnection. Change Initial Catalog to DataService and AttachDBName to DataServicemdf. This step is not required. I just like to keep the file names to be cleaner and do not want to used randomly generated names by Visual Studio.
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=DataService;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\DataService.mdf" />
  • For SQL Server Express LocalDB Only. Right click App_Data directory and add new item. Select SQL Server database file. Name it as DataService.mdf and click OK.
    Add New SQL Server File
  • For this example I have created a table called Movies in the dataabase and populated it with some sample data.The structure of the table is shown below.
    Movies Table Design
  • Right click DataService project and click "Manage NuGet Packages". Make sure that Entity Framework is installed. If not click, search for Entity Framework and install that package.
    NuGet Packages
  • Right click Models folder in DataService project and click "Add new Item". Change the name to "DataServiceModel.edmx" and Click Next.In "Choose Model Contents", select "Generate from database" and click Next.
    Add New Entity Model
  • Choose your data connection for Entities. Select DataService.mdf from the drop down list.Check "Save entity connection settings in Web.Config as" and name it as "DataServiceEntities".
    Choose Data Enity
  • Select tables, views or stored procedures to be included in the model. This example is pretty simple because we only have on table to select called Movies. Keep rest of the options as default and click Finish.
    Choose Entity Table
  • Important: Rebuild the solution before going to next step.
  • Right click the Controllers folder, and click Add → Controller. Us the following values:
    • Controller Name: MoviesController
    • Template: API controller with read/write actions, using Entity Framework
    • Model class: Movie (DataService.Models)
    • Data context class: DataServiceEntities (DataService.Models)
    Click Add to add new controller. The code will written to MoviesController.cs using the scaffolding options specified by us, using LINQ for querying data form the database.
    Add Movies Controller
  • Now run the Visual Studio project. If you are using IIS express a random port has been assigned to it. So in the browser it should open up as http://localhost:<port>. Append "/api/movies" to the URL. If you are using Firefox, it will show XML list of the movies. If you are using Internet Explorer, it will prompt you to save JSON file with movies list.
  • To get the details on individual movie, append id of that movie to the URL. http://localhost:<port>/api/movies/1


We have created a .Net REST web service without actually writing a single line of code. For complex/real life scenarios there will be lot for code. So we have successfully created our data store for Flex, Silverlght and Javascript applications.

No comments:

Post a Comment