Extending the Generated API Controllers
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
In this task, you will extend the generated API controllers by adding custom action methods. You will work with the CarsController:
-
Create a new class with the same name as the name of the controller you want to extend. Use the following naming pattern: <ControllerName>.partial.cs for C# or <ControllerName>.partial.vb for VB. In case of this demo, add a new class named CarsController.partial.cs for C# or CarsController.partial.vb for VB.
Do not modify the original source files generated by the Service Wizard. If you re-generate your controllers later, all changes will be lost. Instead, add your custom methods in a partial class.
The new partial class should be in the same namespace as the original.
- Mark the class with the partial keyword.
-
Add a new action method named GetCarsByMake. It will return all cars filtered by the specified make.
using System.Collections.Generic; using System.Linq; namespace CarRentWebSite { public partial class CarsController { public virtual IEnumerable<Car> GetCarsByMake( string make ) { return this.repository.GetAll().Where( c => c.Make == make ); } } }
Imports System.Collections.Generic Imports System.Linq Namespace CarRentWebSite Partial Public Class CarsController Public Overridable Function GetCarsByMake(ByVal make As String) As IEnumerable(Of Car) Return Me.repository.GetAll().Where(Function(c) c.Make = make) End Function End Class End Namespace
In this task, you extended the generated CarsController by adding a new action method. In the next task, you will learn how to call the Web API from the Web Browser.