Database Reference
In-Depth Information
Listing 9-16. Modifications to RouteConfig Class to Accommodate RPC-Style Routing
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "ActionMethodSave",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
9.
Finally, replace the code in the TravelAgentController with that from Listing 9-17.
Listing 9-17. Travel Agent Web API Controller
public class TravelAgentController : ApiController
{
// GET api/travelagent
[HttpGet]
public IEnumerable<TravelAgent> Retrieve()
{
using (var context = new Recipe3Context())
{
return context.TravelAgents.Include(x => x.Bookings).ToList();
}
}
/// <summary>
/// Update changes to TravelAgent, implementing Action-Based Routing in Web API
/// </summary>
public HttpResponseMessage Update(TravelAgent travelAgent)
{
using (var context = new Recipe3Context())
{
var newParentEntity = true;
// adding the object graph makes the context aware of entire
// object graph (parent and child entities) and assigns a state
// of added to each entity.
context.TravelAgents.Add(travelAgent);
if (travelAgent.AgentId > 0)
{
// as the Id property has a value greater than 0, we assume
// that travel agent already exists and set entity state to
// be updated.
context.Entry(travelAgent).State = EntityState.Modified;
newParentEntity = false;
}
 
Search WWH ::




Custom Search