Create Web API for CRUD operation
So ladies and gentlemen here we will create a new web application
project in which GET, POST, PUT, and delete method will be used for CRUD
operation.
So first we make one, for this you have to go to
the file --> New --> Project then write on name section (whatever you
want) and without disturbing the rest, you just clicked on the OK button.
and then you choose to MVC option in next tab and same as it as
click on OK button.
So, as you can see this our project empty project is ready to launch on
localhost...let's run first before write any code.
For this, Shortcut command press by keyboard
ctrl + F5
As you can see this here are displaying the project in browser by
localhost
URL http://localhost:59322/
Add Models
So before proceeding here on the left side you are able to see some options,
with the help of which we will create controller class,
model class.
Wait, if your Solution Explorer not showing there, don't worry...follow to our steps.
Click on view --> Click Solution Explorer,
Server Explorer option etc.
First we will create class and for this you have go to Models
option and right click on this and go to
Add --> class --> choose first
option (Class) --> change the class name like in our
case (class name is Student) and then click Add. button.
After creating a class then we write some code for generating a table
name or attribute name like StudentID, StudentName
In Student.cs file
-
So, write this command in your code...as it as same and
don't forget to add LINE no 5 code
(System.ComponentModel.DataAnnotations;)
[Key] public int StudentID { get; set; } public string StudentName { get; set; }
NOTE - The get method returns the value of the variable name . The set
method assigns a value to the name variable. The value keyword represents the
value we assign to the property.
Then go to IdentityModels.cs file write this command also.
public DbSet <Student> Student { get; set; }
Save all file (ctrl + s).
Before diving into long sea..let's build first and create a migrations,
add migrations and update-database.
For this Click on
Build --> Build Solution
Then,
Go to Tools -->
NuGet Package Manager --> Package Manager Console
enable-migrations
And at the same time you are able to see that the file is being created
inside the migrations.
add-migration init
update-database
- The Enable-Migration command creates the Migrations folder, which contains a script to initialize the database.
-
- (init is a migration name)
- Update the database for any kind of changes
Now, after updating the database as you can see this
in Server explorer section
Data connection -->
Tables --> Table name (in our case Students is table
name).
Add Web API Controller
So before proceeding here on the left side you are able to see some options,
with the help of which we will create controller class, model class.
For
create a Controller class right click on controller -->
Add --> choose Controller option.
Choose MVC 5 Controller - Empty and then click on Add
button.
This will open Add Scaffold popup as shown below.
Attention Please in Add Controller popup tab, here Controller name is DefaultController. So, Remove "Default" word and type any name whatever you want (in our case we are going to write "Student") then click on Add button.
In StudentController.cs file write some code
for doing a CRUD operation... but first we are doing a Create operation.
Type this command
-
[HttpPost] public ActionResult Create(Student s) { ApplicationDbContext db = new ApplicationDbContext(); db.Student.Add(s); db.SaveChanges(); return View(); }
If in case any error are there like this shown below, then tension we
are here for your help.
Simple mouse click on this red line error
and as you can see that A yellow light bulb appear here so click on
little arrow and choose "using WebApplication2.Models;
(In your case this name may be different, don't worry)
Hurray Error is gone :-) :-p
and "using WebApplication2.Models;" line is added on StudentController.cs file. DONE
Hurray Error is gone :-) :-p
and "using WebApplication2.Models;" line is added on
StudentController.cs file. DONE
Now, we will create a Add view. So, for this you have to right click on
"Create" --> Add view --> and choose the correct all
option as it as same.
then Click on Add button.
This will open Add Scaffold popup as shown below.
In solution explorer, there are appear
Create.cshtml file.
and finally we will run.
ctrl + F5
or
Click on Middle top side Green color
button...click on it.
public ActionResult Display() { ApplicationDbContext db = new ApplicationDbContext(); var lst = db.Student.ToList(); return View(lst); } public ActionResult Details(int id) { ApplicationDbContext db = new ApplicationDbContext(); var s = db.Student.Where(x => x.StudentID == id).SingleOrDefault(); return View(s); }
Please Be Patience We Will Update Very Soon :-)
Disclaimer
All tutorials are for informational and educational purposes only and have been made using our own routers, servers, websites and other vulnerable free resources. we do not contain any illegal activity. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. Hacking Truth is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used. We do not promote, encourage, support or excite any illegal activity or hacking.