Code By Experience

Wednesday, 1 November 2017

If you are newbie in asp.net Web API,
today you will learn Microsoft hottest service technology named “Web API” or
you are expert, please read this post and leave a comment.

So let’s start:

Microsoft Asp.net Web API is a wonderful framework that makes it
easy to build HTTP services that reach a broad range of clients, including
browsers and mobile devices. ASP.NET Web API is the best platform for building
RESTful applications on the .NET Framework.
Web Api service not impose client-side object proxy requirements. One of
the best things is it supports JSON and XML formats out-of-the-box for
data transfer over the wire.

Buddy for your understanding, express a real scenario:

“We have a student list for different
Institutes/schools where each institute/school is under an Education board.
Each student can choose only one group form “Science, Business Studies and
Humanities” that means a student can study only one group. Students have
subject-wise number who already achieved by examination.”

I think you understood now, this system containing student
information with school, board, group and also subject wise results.

The government wants to expose a service only for a few days to
publish the results with basic parameters.

So, in this regard, I will make an Asp.net Web API service where
student select their own board and also give own role no and service will
provide them subject wise Grade.

For this above scenario, we find out Entities:

Education Board

Institute

Group

Subject

Student

ResultSheet

Figure: ERD diagram

I will not create any
database for this. We will make temporary data in TempData class. Conceptually
we make relational data list.

Here we will use
Visual Studio 2015.

Let’s go enjoying……….😊

Start Visual Studio and select New
Project from the Start page. Or, from
the File menu, select New and
then Project.

Simply saying, Model
used to back response under the client request. Model is an object that Web API can automatically serialize your model
to JSON, XML, or some other format, and then write the serialized data into the
body of the HTTP response message.

In asp.net Web API, the controler handled all HTTP request.
Now we will add StudentResultControllercontroller that
hold two methods one return all Board
and another return strudent result
by role no. In the solution explorer
right click Controller , Add Controller.

In the New opened wizard Add
Scaffold ,select Web API 2 Controller –Empty and Click Add

In the Add
Controller dialog, name the controller
"StudentResultController". Click Add.