Click here to Skip to main content
15,877,754 members
Articles / Web Development

Web API Return List of Custom Type (JSON)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
3 Oct 2016CPOL 59.6K   4   5
In this tip, we will learn how to return list of custom type (JSON) data using ASP.NET Web API.

Let's Start

Start Visual Studio and select a new project from the start page. Or from the File menu, select New and then Project. In the Templates pane, select Installed Templates and expand Visual C# node and select web. In the list of project Templates, select ASP.NET Web Application and click OK. And give your Project name Like StudentApp.


In the new ASP.NET Project dialog, select Web API and Click Ok.


We got StudentApp (our Application Name) application with Installed all need Files.


Add Model: Click Model Folder, add Student and ResponseModel Class within Model Folder Student Class:

public class Student
       public int ID { get; set; }
       public string Name { get; set; }
       public string Address { get; set; }
       public string Email { get; set; }

ResponseModel Class

public class ResponseModel
       public string Message { set; get; }
       public bool Status { set; get; }
       public object Data { set; get; }

Actually Student Class we use for Student Property and ResponseModel Class we use for Return Type. we will pass all students within ResponseModel Class. After a few minutes, we will see how to use this Class.

Add Controller: Click Controller Folder, right button click, add click, click Controller and Select Web API 2 Controller, click Add and give your Controller Name (StudentController).

namespace StudentApp.Controllers
    public class StudentController : ApiController
        public ResponseModel Get()
            ResponseModel _objResponseModel = new ResponseModel();
            List<Student> students = new List<Student>();
            students.Add(new Student {
                ID = 101,
            students.Add(new Student
                ID = 102,
                Name = "Mitila",
                Email = "",
                Address = "Dhaka,Bangladesh"
            students.Add(new Student
                ID = 104,
                Name = "Popy",
                Email = "",
                Address = "Dhaka,Bangladesh"

            _objResponseModel.Data = students;
            _objResponseModel.Status = true;
            _objResponseModel.Message = "Data Received successfully";

            return _objResponseModel;

And run, give url (/api/student), we can see output: outApi


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer Amber Software Solution Ltd.
Bangladesh Bangladesh
Hi, I am Shamim Uddin.Working with Microsoft Technologies.

Comments and Discussions

PraiseExcellent Pin
CybrRob20-Oct-20 10:00
professionalCybrRob20-Oct-20 10:00 
QuestionWeb API return type Pin
Member 1308740422-Feb-19 7:55
Member 1308740422-Feb-19 7:55 
Questionnice share Pin
M,AqibShehzad5-Oct-16 0:07
professionalM,AqibShehzad5-Oct-16 0:07 
AnswerRe: nice share Pin
Shamim Uddin5-Oct-16 2:09
professionalShamim Uddin5-Oct-16 2:09 
PraiseMy vote of 5! Pin
jediYL4-Oct-16 10:10
professionaljediYL4-Oct-16 10:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.