Click here to Skip to main content
13,259,539 members (52,982 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.9K views
3 bookmarked
Posted 3 Oct 2016

Web API Return List of Custom Type (JSON)

, 3 Oct 2016
Rate this:
Please Sign up or sign in to vote.
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.

1

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

2

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

3

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,
                Name="Seam",
                Email="seam@gmail.com",
                Address="Dhaka,Bangladesh"
            });
            students.Add(new Student
            {
                ID = 102,
                Name = "Mitila",
                Email = "mitila@gmail.com",
                Address = "Dhaka,Bangladesh"
            });
            students.Add(new Student
            {
                ID = 104,
                Name = "Popy",
                Email = "popy@yahoo.com",
                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

License

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

Share

About the Author

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

You may also be interested in...

Pro

Comments and Discussions

 
Questionnice share Pin
M,AqibShehzad5-Oct-16 1:07
professionalM,AqibShehzad5-Oct-16 1:07 
AnswerRe: nice share Pin
Shamim Uddin5-Oct-16 3:09
professionalShamim Uddin5-Oct-16 3:09 
PraiseMy vote of 5! Pin
jediYL4-Oct-16 11:10
professionaljediYL4-Oct-16 11: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 3 Oct 2016
Article Copyright 2016 by Shamim Uddin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid