Introduction
We will learn Scaffolding in Web API 2 in this article. Scaffolding is a Text Template Transformation and Toolkit (T4) based code generation framework for ASP.NET. It is a template-based code generator that has been part of Visual Studio since version 2005.
Before reading this article, kindly see my previosu articles on Web API 2, perhaps these articles will make the concept clearer.
Scaffolding is a technique that means "Quick generation of basic template of our application that can be edited and customized the application easily". ASP.NET MVC provides auto generated Views, Controllers, Database, Table Script and so on. All those layers are generated based on the Model. The field that we need to show or hide in the view can be controlled by the [ScaffoldColumn] attribute.
Scaffolding only gives the basic layout and with this layout we can customize our application, like Create, Read, Update and Delete (CRUD) operations. We create a Model and View for all functions and Controllers with each action item, so we need to write lots of code. Instead of writing this code scaffolding provides a feature to just select a template and model (created as per entity) and automatically creates a controller with all methods and supports a View.
Scaffolding is a Dynamic Data Element that automatically generates code for the Web API. There are some benefits of Scaffolding.
- Less coding
- CRUD Operations enabled along with sorting and paging
- Data Validation
- Data filter, automatically create foreign key, Boolean fields and enumeration fields
When we are choosing Web API 2, we got some basic templates, some of them are the following:
- Web API 2 Controller -Empty
- Web API 2 Controller with actions, using Entity Framework
- Web API 2 Controller with read/write actions
- Web API 2 OData Controller with action, using Entity Framework
- Web API 2 OData Controller with read/write actions
Let's start to learn step-by-step, how to create a Scaffolding in Web API 2.
Prerequisites
There are the following things we need to use when developing a Web API 2 application.
- Visual Studio 2013
- ASP.NET Web API 2
Getting StartedIn this section we will follow some important steps and these are:
- Create ASP.NET Web API
- Add Web API 2 Controller
- Add a class inside the model
There are some basic procedures to follow when creating a Web API along with MVC using Visual Studio 2013, we need to also select the Web API Template.
Step 1
Open the Visual Studio 2013 and click New Project.
Step 2
Select the ASP.NET Web Application and provide a nice name for the project.
Step 3
Select the Web API template and click the OK button, by default it will choose MVC along with the Web API.
Step 4
Right-click on the Model folder and add a Student class to the model where we define all the necessary fields. In the following image, I have defined some fields like Id, name, Address and College Name.
- public class Student
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- public string CollegeName { get; set; }
-
- }
Step 5
Add
a new controller using scaffolding. Right-click on the Controller folder and add the controller and click the Add button after selecting the desired template.
Step 6Select the Model class that we created and click on the
"+" button and edit the name of the New data context type. Provide a meaningful name for the controller.
This is the simple procedure to create Scaffolding Web API 2. In the code above we can see that all verbs for a service are being created using Scaffolding. In future articles we will learn further.
Further Reading<< Getting Started with ASP.NET Web API 2 : Day 5