Query String Parameters is an important concept in .NET programming. Let us learn how to use query string parameters in ASP.NET Web API programming. Follow the steps mentioned below.
Open SQL Server 2014 or a version of your choice, create a table, and insert some records.
Insert records into the SQL table, as shown in the following code.
Step 3
Open Visual Studio 2017, click on New Project, and create an empty Web API application project.
After clicking on the "New Project" link, a window will appear. Select "Web" from the left panel, choose ASP.NET Web application, give a meaningful name to your project, and then click OK, as shown in the below screenshot.
After clicking on OK, one more window will appear; choose Empty, check on Empty Web API checkbox and then, click OK, as shown in the following screenshot.
After clicking OK, the project will be created with the name you have given. In my case, it is WebAPIQueryString_Demo.
Step 4
Add Entity Framework now. For that, right-click on Models folder, select Add >> New Item, then, click on it.
Again, you will get a window, from where you need to select "Data" from the left panel and choose ADO.NET Entity Data Model. Give it a name, like DBModel (this name is not mandatory, you can give any name) and click "Add".
After you click on "Add a window", the wizard will open. Choose EF Designer from the database and click "Next".
After clicking on "Next", a window will appear. Choose "New Connection". Another window will appear; add your server name here. If it is local, then enter dot (.) instead of the server name. Choose your database and click "OK".
The connection will be added. If you wish to save the Connection, you can opt for it. You can change the name of your connection like below. It will save the connection in web.config. Click "Next".
In the next window, choose database table name as shown in the below screenshot, and click "Finish". Entity Framework will be added to your project and the respective class gets generated under "Models" folder.
The following class will be added.
- namespace WebAPIQueryString_Demo.Models
- {
- using System;
- using System.Collections.Generic;
-
- public partial class Employee
- {
- public int EmployeeId { get; set; }
- public string Name { get; set; }
- public string Gender { get; set; }
- public Nullable<int> Age { get; set; }
- public string Position { get; set; }
- public string Office { get; set; }
- public Nullable<System.DateTime> HireDate { get; set; }
- public Nullable<int> Salary { get; set; }
- }
- }
Step 5
Right-click on the Controllers folder, select Add >> Controller, as shown in the below screenshot.
After clicking on Controller, a window will appear. Choose Web API 2 Controller-Empty and click "Add".
Another window will appear with a name DefaultController. Change the name to EmployeeController and click "Add". The EmployeeController will be added under the Controllers folder. Remember, you don’t have to change the Controller suffix for all the controllers. You should change only the first part of the name, i.e., in place of Default, just change Employee, as shown in the below screenshot.
Complete code for Controller
Build and run the project by pressing Ctrl+F5. The following is the output.