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".
Step 2
The Solution Explorer appears.
Step 3
In the Solution Explorer, right-click on the ApplicationName (HtmlApplication) and choose "Add Client".
Step 4
In the Solution Explorer, right-click on the Server and choose "Add Table".
Step 5
The table appears.
Step 6
In the Solution Explorer right-click on the HTMLClient Screen and choose "Add screen".
Step 7
The Add New Screen dialog box appears on the screen. Add a Browse Screen to display the list of Employees.
Step 8
Add another Screen (Add/Edit Detail Screen).
Step 9
Open the BrowseEmployees Screen, the BrowseEmployees Screen Designer appears.
Step 10
Add a button to the BrowseEmployees Screen.
Step 11
The Add Button dialog box appears.
Step 12
Press F5 to run the application and add some employee's data.
Step 13
Go to the Solution Explorer and click on the "Toggle View" button and select "File View".
Step 14
Go to the Solution Explorer and right-click on the Server project and click on "Add" and choose "Add New Item".
Step 15
The Add New Item dialog box appears. Choose the "WebAPI Controller" template and provide a name for it.
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 .
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.