Introduction to ASP.NET Web API
“ASP.NET Web API is a framework that simplifies the creation of HTTP services“
Using ASP.NET Web API we can create HTTP services those are non-SOAP based like plain XML or JSON strings etc. with added advantages.
- Allowing to create resource-oriented services using the full features of HTTP.
- Exposing services to a variety of clients easily like browsers or mobile devices etc.
- Learn the Foundation: HTTP & REST Concepts
- Resources and URI
- HTTP Methods
- HTTP Status Codes
- HTTP Content
- Internet Media Types
- REST
- JSON and XML
- Creating ASP.NET Web API service that supports CRUD (Create, Retrieve, Update, Delete) operations
- Building a Real World Application using ASP.NET Core and Angular 2
Apart from Visual Studio 2010 or 2012, we also need MVC 4.0 to implement this HTTP service. For the purpose of this implementation, I am going to use Visual Studio 2010.
You can download MVC 4.0 for Visual Studio 2010 from Microsoft as follows:
Following are 3 simple steps to create HTTP service that returns non-SOAP based data.
- Create Web API Project
- Prepare domain Model
- Adding Controller class
- A. GET
- B. POST
- C. DELETE
- D. PUT
To further test your ASP.NET Web API skill, Take a Complete FREE Online Test or MCSD Practice Exam: 70-486 (Developing ASP.NET MVC Web Applications). Simply Click Here.
Correct Answer: D
ASP.NET Web API Tutorial with Simplified Approach
1. Create Web API Project
- Open Visual Studio and create “New Project” i.e. File -> New Project.
- Choose “ASP.NET MVC 4 Web Application” template and name project as “FirstWebAPIService”.
- When you click “OK” button, a new window will appear for selecting a sub-template. Actually for ASP.NET MVC 4 Web Application, we have multiple sub-options i.e. Empty, Internet Application, Web API etc.
- Choose “Web API” and simply press “OK” button.
- A default ASP.NET MVC 4 Web API template project is created. As its an MVC application template, so you will easily find “Model”, “View” and “Controller” folders inside it.
- Right click on the “Model” folder and choose “Class” under “Add” from the context menu as shown in figure.
- Name the class as “Product.cs”.
1 2 3 4 5 6 7 |
public class Product { public int ProductID { get; set; } public string ProductName { get; set; } public string ProductCategory { get; set; } public int Price { get; set; } } |
Controller class plays an important role, because request coming from client hits the controller first. Then the controller decides which model to use to serve the incoming request. So, in order to add a controller:
- Right click on the “Controller” folder and choose “Controller” under “Add” from the context menu as shown in figure.
- Name the controller as “ProductsController”.
- Click the “Add” button, a new controller class will be added.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class ProductsController : ApiController { Product[] products = new Product[] { new Product { ProductID = 1, ProductName = "Product 1", ProductCategory= "Category 1", Price = 120 }, new Product { ProductID = 2, ProductName = "Product 2", ProductCategory= "Category 1", Price = 100 }, new Product { ProductID = 3, ProductName = "Product 3", ProductCategory= "Category 2", Price = 150 }, new Product { ProductID = 4, ProductName = "Product 4", ProductCategory= "Category 3", Price = 90 } }; public IEnumerable<Product> GetProducts() { return products; } } |
Now, it’s time to test your HTTP service using ASP.NET MVC Web API.
Run the application by pressing “CTRL+F5”, Welcome window will appear as follows:
In order to call our Product controller, change the URL as “http://localhost:XXXX/api/products”. You will see the results as shown in following output window.
- A. Use ViewBag.
- B. Use Partial View against Web API method.
- C. You can’t return view from ASP.NET Web API method.
- D. None of Above.
To further test your ASP.NET Web API skill, Take a Complete FREE Online Test or MCSD Practice Exam: 70-486 (Developing ASP.NET MVC Web Applications). Simply Click Here.
Correct Answer: C
Hopefully, this simple web api tutorial will be helpful for developers to code their first HTTP service using ASP.NET MVC Web API. Further, for performing all CRUD operations using Web API, Follow the click. Soon you will be able to download this ASP.NET Web API Tutorial PDF.
- We have just released a complete article series on Building an ASP.NET MVC Shopping Cart using MVC, C#, Entity Framework and SQL Server. Keep in touch to get the knowledge that how to create Online Shopping Store in ASP.NET MVC?, along with complete Source Code.
- The Complete ASP.NET MVC 5 Online Course : A review of a Best Seller ASP.NET MVC 5 by Mosh Hamedani (an Instructor with 5 star rating) that takes you from the beginning and helps you developing professional MVC Applications. Take this Course Now on Discounted Price
Other Related Articles:
Top 10 Interview Questions and Answers Series:
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
wow…seems great
Really great post .. thanks sharing your topic
website
Thanks very much… I m waiting for CRUD operations using Web API…. Thanks again….
Hi Jalpan,
Hopefully you have already checked out CRUD operations already at following URL.
————————————–
http://www.topwcftutorials.net/2014/01/CRUD-WCF-RESTful-Service.html
—————————–
Easy to do tutorial. Thanks mate however I am having this error when I run the program:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
No HTTP resource was found that matches the request URI ‘http://localhost:59074/api/products’.
No type was found that matches the controller named ‘products’.
Dear Abraham,
You need to check you configuration and verify the routeTemplate as follows:
1. Open WebAPIConfig.cs file under “App_Start” folder in Visual Studio.
2. Inside Register method, routeTemplate value should be equal to “api/{controller}/{id}”.
As I have the following:
config.Routes.MapHttpRoute(
name: “DefaultApi”,
routeTemplate: “api/{controller}/{id}”,
defaults: new { id = RouteParameter.Optional }
);
Thanks for the swift reply Imran.
It looks like I have the same settings under Register Method, find code below:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: “DefaultApi”,
routeTemplate: “api/{controller}/{id}”,
defaults: new { id = RouteParameter.Optional }
);
Abraham, Kindly follow the thread.
http://forums.asp.net/t/1916587.aspx?WebAPI+No+HTTP+resource+was+found+that+matches+the+request+URI
when i try this…everything was ok till chnging url bt when i change url it land at google search page …why this??
Dear Nilam,
Kindly look for my above comments on Abraham query. You might need to update routeTemplate.
If it doesn’t resolve your issue, kindly elaborate your issue a bit more.
This comment has been removed by a blog administrator.
i am getting error as MvcApplication1.Controllers.productsController.Product’ is a ‘field’ but is used like a ‘type. Plzz help me out in this problem
I believe you are making some mistake while calling a method. This type of error occurs normally when someone calls a method at class level instead of making call inside a method.
Above are around 52 errors, If you can remove others, hopefully things will be clear but it seems you have some wrong placement of methods (outside class) etc.
IMRAN, could you tell me the way how to allow update/delete/create records (through web service) only for authenticated users?
Hi Roman,
CRUD Operations with same approach (i.e using ASP.NET Web API) is here:
http://www.webdevelopmenthelp.net/2013/12/crud-operations-using-web-api.html
CRUD Operations using RESTful Services:
http://www.topwcftutorials.net/2014/01/crud-wcf-restful-service.html
For Authentication, you can follow here:
https://msdn.microsoft.com/en-us/library/vstudio/w67h0dw7(v=vs.100).aspx
Imran, this blog is excellent. Conglaturations !!
well explained! easy to understand for a newcomer.
here one thing i want to mention is while executing above given example my browser was showing below error :
“This XML file does not appear to have any style information associated with it. The document tree is shown below.
No HTTP resource was found that matches the request URI ‘http://localhost:45788/api/products’.
No type was found that matches the controller named ‘products’.
”
And the reason i found was, in my application, ‘ProductsController’ class was by default inheriting “Controller” as base class as given below:
“public class ProductsController : Controller”
instead of ‘ApiController’ class as shown in above example code.
So After adding namespace “using System.Web.Http;” in ‘ProductsController’ class and inheriting ‘ApiController’ as base class as follows:
“public class ProductsController : ApiController”
it worked.
thanks again for this good piece of work.
well explained! easy to understand for a newcomer.
one thing i want to mention, while executing above given example browser was showing below error :
“This XML file does not appear to have any style information associated with it. The document tree is shown below.
No HTTP resource was found that matches the request URI ‘http://localhost:45788/api/products’.
No type was found that matches the controller named ‘products’.
”
And the reason i found was, in my application, ‘ProductsController’ class was by default inheriting “Controller” as base class as given below:
“public class ProductsController : Controller”
instead of ‘ApiController’ class as shown in above example code.
So After adding namespace “using System.Web.Http;” in ‘ProductsController’ class and inheriting ‘ApiController’ as base class as follows:
“public class ProductsController : ApiController”
it worked.
thanks again Imran for this good piece of work.