Chapter 5. Working with ASP.NET 4.5

In this chapter, we will discuss the new features in ASP.NET 4.5 and explore how we can work with the ASP.NET Web API. The ASP.NET Web API is a lightweight alternative approach like WCF, and uses HTTP as the application protocol.

In this chapter, we will cover the following points:

  • Working with the OData protocol
  • New features in Microsoft .NET Framework 4.5
  • New features and enhancements in ASP.NET 4.5
  • Working with the ASP.NET Web API

Working with the OData protocol

The Open Data protocol (OData) is a protocol that is built on web standards, such as HTTP, Atom, and JSON, and standardizes how the data is exposed and consumed. It is a data access protocol that provides a uniform way of performing CRUD operations on the data. It is used to expose and access information from different data sources; that is, relational databases, filesystems, content management systems, and so on. OData is a standardized protocol that builds on top of core protocols, such as HTTP and architecture paradigms, such as REST. Like RSS, Atom is a way to expose feeds. Note that AtomPub makes use of HTTP verbs such as GET, POST, PUT, and DELETE, to facilitate publishing of data.

Working with the ASP.NET Web API and OData

In this section, we will explore how we can work with the ASP.NET Web API and OData. To install the Web API OData Controller template, right-click on the Controllers folder in the solution explorer window, and then select New Scaffolded Item. Next, select Web API 2 OData Controller with actions using the Entity Framework.

To work with the ASP.NET Web API and OData follow these steps:

  1. Open the Visual Studio 2013 IDE.
  2. Create a new project and save it with a name.
  3. In the solution explorer, right-click and select New Item.
  4. Create an Entity Data Model.
  5. Configure the OData route.
  6. Implementing the OData controller.

Note

Note that if you would like to expose an endpoint that adheres to the OData protocol, you should extend your Controller class from ODataController. On the contrary, if you would like to have a REST endpoint, you should extend your Controller class from ApiController. Also, you can host multiple OData endpoints vis-a-vis your non-OData endpoints.

The following code snippet illustrates how the EmployeeController class would look:

public class EmployeeController : ODataController
{
  List<Employee> _employees = DataStore.Employees;

  [Queryable]
  public IQueryable<Employee> GetEmployees()
  {
    return _employees.AsQueryable();
  }

  public Employee GetEmployee([FromODataUri] int employeeID)
  {
     return _employees[employeeID];
  }
}

The [Queryable] attribute facilitates OData query syntax on a particular action. You can also derive your controller class from EntitySetController—a base class for exposing entity sets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset