How to Add WebAPI Support to the Server Project in LightSwitch 2012

This article describes how to add WebAPI support to a server project in LightSwitch using Visual Studio 2012.

Procedure for adding WebAPI support to the server project

Step 1

  • Open the Visual Studio 2012.
  • Go to "File" => "New" => "Project..."
  • In "New Project" => "Installed" => "Template"
  • In "Template" => "LightSwitch"
  • Select "LightSwitch Application (Visual C#)".
  • Enter the Name and choose the location.
  • Click "OK".

lightswitch app.jpg

Step 2

The Solution Explorer appears.

solu ex.jpg

Step 3

In the Solution Explorer, right-click on the ApplicationName (HtmlApplication) and choose "Add Client".

add client.jpg

Step 4

In the Solution Explorer, right-click on the Server and choose "Add Table".

add table.jpg

Step 5

The table appears.

table.jpg

Step 6

In the Solution Explorer right-click on the HTMLClient Screen and choose "Add screen".

add screen.jpg

Step 7

The Add New Screen dialog box appears on the screen. Add a Browse Screen to display the list of Employees.

Browse Data Src.jpg

Step 8

Add another Screen (Add/Edit Detail Screen).

add edit add new src.jpg

Step 9

Open the BrowseEmployees Screen, the BrowseEmployees Screen Designer appears.

browse screen designer.jpg

Step 10

Add a button to the BrowseEmployees Screen.

add button.jpg

Step 11

The Add Button dialog box appears.

add new button.jpg

Step 12

Press F5 to run the application and add some employee's data.

output1.jpg

Step 13

Go to the Solution Explorer and click on the "Toggle View" button and select "File View".

toggle view.jpg

Step 14

Go to the Solution Explorer and right-click on the Server project and click on "Add" and choose "Add New Item".

add new item.jpg

Step 15

The Add New Item dialog box appears. Choose the "WebAPI Controller" template and provide a name for it.

web Api controller class.jpg

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

using System.Collections;

using Microsoft.LightSwitch;

 

namespace LightSwitchApplication

{

    public class EmployeeController : ApiController

    {

        // GET api/<controller>

        public IEnumerable<string> Get()

        {

            return new string[] { "1", "2" };

        }

 

        // GET api/<controller>/5

        public string Get(int id)

        {

            return "value";

        }

 

        // POST api/<controller>

        public void Post([FromBody]string value)

        {

        }

 

        // PUT api/<controller>/5

        public void Put(int id, [FromBody]string value)

        {

        }

 

        // DELETE api/<controller>/5

        public void Delete(int id)

        {

        }

    }

}

Step 16 

Now we need to add a global application class to the server project to add an HTTP route to our WebAPI .

global add new item.jpg


using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.SessionState;

using System.Web.Routing;

using System.Web.Http;

 using System; 

namespace LightSwitchApplication

{

    public class Global : System.Web.HttpApplication

    {

 

        protected void Application_Start(object sender, EventArgs e)

        {

            RouteTable.Routes.MapHttpRoute(

        name: "DefaultApi",

        routeTemplate: "api/{controller}/{id}",

        defaults: new { id = System.Web.Http.RouteParameter.Optional }

        );

        }

 

        protected void Session_Start(object sender, EventArgs e)

        {

 

        }

 

        protected void Application_BeginRequest(object sender, EventArgs e)

        {

 

        }

 

        protected void Application_AuthenticateRequest(object sender, EventArgs e)

        {

 

        }

 

        protected void Application_Error(object sender, EventArgs e)

        {

 

        }

 

        protected void Session_End(object sender, EventArgs e)

        {

 

        }

 

        protected void Application_End(object sender, EventArgs e)

        {

 

        }

    }

}

Step 17

If you run the application again, you can browse to "~/api/employees" under the application root in order to get the result.

output.jpg


Similar Articles