Web API is a programming interface which exposes endpoints
that define request and response eco system. Each method can contain in
different types of request like POST, GET, PUT, DELETE etc. request should be
based the way which the API is built.
Here Is the simple example to create simple POST method that
inserts some data into database using Entity framework.
1.
Create new Project with WEB API as below
This will create the new project
of WEBAPI in MVC framework.
2.
Right Click on the controllers and add new WEB API
Controller as shown below.
Name your WEB API controller accordingly, I have named it as
EmployeeController.
3.
Right click on the Model folder and add ADO.NET
Entity Data Model as shown below.
Entity
Data Model wizard asks you to select what the model should contain, Select EF
designer from Database as shown below.
Click
on Next and Choose Data Connection by selecting new connection.
After Clicking New Connection, it
will prompt the new popup to enter server details, userid, password and
selecting the database.
After test connection is successful
click on OK and your connection string will get generated as shown below
Select the Entity framework version
and Click the Next button
Select the Table that is necessary for accessing the data as
shown below.
Click on Finish that will
generate the EDMX file with table name and columns as shown below.
4.
On EmployeeController.cs we can find multiple
functions remove all the except POST
method and replace the code as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using FirstWebApiDemo.Models;
namespace FirstWebApiDemo.Controllers
{
public class EmployeeController : ApiController
{
public HttpResponseMessage Post([FromBody]EmployeeDetail employee)
{
try
{
using (ACT2_MINIQEntities entities = new ACT2_MINIQEntities())
{
entities.EmployeeDetails.Add(employee);
entities.SaveChanges(); //inserts data in to DB
var message = Request.CreateResponse(HttpStatusCode.Created, employee); //Setting Response Headers
return message; //Returns Http Response
}
}
catch (Exception exception)
{
//handling Exceptions
return
Request.CreateErrorResponse(HttpStatusCode.BadRequest, exception);
}
}
}
}
To Test Output Use Fiddler Or POSTMAN Rest-client.
Output :
16 Comments
Thanks for sharing this informative blog.
ReplyDeleteRegards,
DOT NET Training Institutes in Chennai|Best DOT NET Training in Chennai
Great Article
ReplyDeleteDot Net Based Projects for Final Year Students
FInal Year Project Centers in Chennai
JavaScript Training in Chennai
Dot Net Training in Chennai
This is an awesome post for the people who are in search of this technology.
ReplyDeleteYour blog is really mind blowing and the design is really first class.
dot net training in chennai
dot net course in chennai
salesforce training in chennai
sas training in chennai
spoken english classes in chennai
dot net training in velachery
dot net training in tambaram
dot net training in anna nagar
I had got a clear idea of what the blog is all about. Kindly continue doing more.
ReplyDeleteSpoken English Classes in Bangalore
English Speaking Course in Bangalore
Best Spoken English Classes in Bangalore
English Classes in Bangalore
Spoken English Class in Bangalore
English Speaking Classes in Bangalore
Spoken English Classes in Coimbatore
Spoken English Classes in Chennai
While seeing your way of writing I am dawn sure that you have a great scope as a content creator.
ReplyDeleteSEO Training in Chennai
JAVA Training in Chennai
Big Data Training in Chennai
Selenium Training in Chennai
German Classes in chennai
DOT NET Training in Chennai
Dot Net Training in OMR
.Net course in chennai
Thanks for the great article this is very useful info thanks for the wornderful post,
ReplyDeleteJava development company
whatsapp group links list
ReplyDelete
ReplyDeleteyour explanation way is too good, very easy to understand for all readers. Thank you for providing the best details and I want more unique posts.
Primavera Training in Chennai
Primavera Course in Chennai
Tableau Training in Chennai
Spark Training in Chennai
Power BI Training in Chennai
Excel Training in Chennai
Oracle Training in Chennai
Oracle DBA Training in Chennai
Social Media Marketing Courses in Chennai
Good explanation with appropriate solution.
ReplyDeleteArticle submission sites
Technology
This is an important article. I read like this type of article only. You mentioned more important points about technology. Thankyou.
ReplyDeletecore java training in chennai
C Language Training in Chennai
c++ class
c c++ courses in chennai
C Language Training
javascript course in chennai
Appium Training in Chennai
JMeter Training in Chennai
Great job. Keep updating this article by posting new informations.
ReplyDeleteSpoken English Classes in Chennai
English Coaching Classes in Chennai
Japanese Language Classes in Chennai
French Language Classes in Chennai
pearson vue exam centers in chennai
German Classes in Chennai
Spoken English Classes in Tnagar
Spoken English Classes in OMR
This is really impressive post, I am inspired with your post, do post more blogs like this, I am waiting for your blogs.
ReplyDeleteaviation training in Chennai
air hostess academy in Chennai
Airport Management Training in Chennai
airport ground staff training courses in Chennai
Aviation Academy in Chennai
air hostess training in Chennai
airport management courses in Chennai
ground staff training in Chennai
Thanks for this article, your post helps me to understand the future and career prospects. Keep updating your blog with the latest information.
ReplyDeleteSalesforce Training in Chennai
Salesforce course in Chennai
AngularJS Training in Chennai
ccna course in Chennai
PHP Training in Chennai
gst Training in Chennai
Tally course in Chennai
Salesforce Training in Anna Nagar
Salesforce course
You have posted suvh an awesome blog! thanks for sharing this information..
ReplyDeletepearson vue exam centers in chennai
pearson vue test center in chennai
German Classes in Chennai
IELTS Coaching in Chennai
Spoken English Classes in Chennai
Japanese Classes in Chennai
TOEFL Coaching in Chennai
pearson vue exam centers in OMR
pearson vue exam centers in Porur
pearson vue exam centers in Adyar
Very nice and interesting blog. You can also check my articles as well.
ReplyDeleteweb development vs android development
selenium webdriver methods
php programming
ethical hacker job opportunities
devops interview questions and answers for freshers
hacking certification
Thanks for the information, it's very nice
ReplyDeleteWheel Alignment in Perth