This basic steps will teach you how to create and implement basic Asp.net Web Service API.
What is Web APIs(Application Programming Interface) means?
Read more here
Since our focus is the .Net Framework, we will use ASP.NET Web API implementation.
What is ASP.NET Web API?
To get started on ASP.NET web api, read more here: https://www.asp.net/web-api
Note: This steps are based on the way how I create ASP.NET Web API using Visual Studio. You can use whatever steps that you want but make sure it will end up the same outputs.
1. Step 1.0 - Creating the project
1.1 Open the Visual Studio
1.2 File -> Project (Ctrl+Shift+N)
1.3 When the dialog box open, choose Templates -> Visual C# -> Web. We name our project as “MyWebAPI”
1.4 Another dialog box will appear and just choose Web API
1.5 Welcome page will appear
Step 2.0 - Updating our Web.config for DB connection
2.1 Open the Solution Explorer and locate the Web.config file
Like in basic ASP.NET web app, the connection snippet is the same with ASP.NET Web API.
2.2 Put connection string
1
2
3
<connectionStrings>
<add name="MySampDBContext" connectionString="Data source=YOUR_DB_CONNECTION;Initial Catalog=YOUR_DB_NAME; Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Step 3.0 - Creating your class model
3.1 Right click the Model folder, choose Add and choose “Class…” In this example, we name our model class as “UserDetailCredentials”
3.2 Adding properties inside the class
1
2
3
4
5
6
7
public class UserDetailCredentials
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Name { get; set; }
}
3.3 Rebuild the solution (Ctrl+Shift+B)
4. Step 4.0 - Creating your MVC & API Controllers
In this tutorial, since we want to track what are the details that will appear on our API, let us use the MVC Controller with views, using Entity Framework. Let this serve us our UI to make this easier.
- Creating MVC Controller with views, using Entity Framework
4.1 Right click the Controllers folder, choose Add and Choose “Controller…”
4.2 Dialog controller will open, choose the MVC 5 Controller with views, using Entity Framework.
4.3 Adding Controller. Choose your model, Check the “Use async controller actions” and the Data Context Class(“name” that we included in the connection string). Click Add
Now we have our MVC controller and our UI pages, we can now create our API controller.
4.5 Rebuild the solution.
- Creating API Controller
4.6 Right click the Controllers’ folder, choose Add and Choose “Controller…”
4.7 Dialog controller will open, choose the Web API 2 Controller with actions, using Entity Framework.
4.8 Adding Controller. Choose your model, Check the “Use async controller actions” and the Data Context Class(“name” that we included in the connection string) and we name our api controller as “UserDetailCredentialsAPIController”. Click Add
Now we have controller class with actions for our api.
Here’s the summary of the default api contorller that we created.
5.0 Enabling Migrations and updating the database
Now we already prepared everything, it’s time to enable migrations and update the database.
5.1 Open Package Manager Console
5.2 Type in “Enable-Migrations” (without quotes) and click Enter
5.3 Adding migration by typing in “Add-Migration (YourMigrationName)” (without quotes) and press Enter
The preview of your migration will appear. When you are done inspecting, you can now proceed to update the database.
5.4 Updating the database by simply typing “update-database” and press the Enter
6.0 Running the whole project
6.1 Build and Start running the project.
Add sample details
6.2 Create a new set of details
- Accessing the api page
Now we have set of details, let’s us now view our custom API page.
In order to access the api, by default, you need to set the uri to api followed by your api controller name
1
http://yourhost/api/yourapicontrollername
6.3 Go the api link
JSON Format using Microsoft Edge
XML Format using Google Chrome
Here’s the Github Link For This Post
Give me a ★ :D
Thank you for reading. I hope you’ve learned from me. Feel to comment below!