Build and Consume an ASP. Net MVC 4 Web APIWEBINAR: On- demand webcast. How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2. REGISTER > ASP. Net MVC 4 is packed up with a new project template called Web API. In the technology world, an API is defined as an exposed application interface, which can be consumed by different client applications in order to perform data transfers. Similarly ASP. Net MVC 4 Web API application can be used to create HTTP services, which can be consumed by a wide range of clients from mobile browser applications to Windows Forms applications. In this article I will give an overview on creating an ASP. Net MVC 4 Web API and consuming it from client applications.
Fig 1. 0 New ASP. NET MVC 4 Project. How Different it is from . NET Web Services. When I first heard about the Web API feature, instantly my thought was about how different it would be from a regular XML web service. In this article, we will discuss how to implement Angular Data Table with (Insert, Update, and Delete) Short Form (CRUD) using Asp.net MVC + Angular JS in one page. ![]() Following are a few things in which Web API is different from web services. Works over plain HTTP protocol as HTTP services without SOAP. Content Negotiation – Automatically responds back in plain XML or in JSON format based on the Accept Header in the client request. Allows implementing REST (Representative State Transfer) architecture. URL can be used to define the actions and parameters. Support for HTTP verbs. GETb. POSTc. PUTd. DELETEAsp. Net MVC Application as Web APIWhen you create an ASP. Net MVC Web API application you will not see any considerable difference between an ASP. NET Web API and a normal MVC application because both exhibit the same MVC architecture. So it is important to know what is extra for a Web API than a normal MVC application. Edit Grid In Mvc 4 Vs Mvc 5Api. Controller. Though both the application types are driven through controller, the Web API controller classes inherit from the Api. Controller class. The action methods return only data instead of Views and also they are mapped to a particular HTTP operation (get, post, put or delete) based on their prefix. When a Web API project is created a default controller class named Values. Controller. cs will be added under the controller folder. Web. Api. Config. The Web API request routing is registered in this class. It allows the application to differentiate if the request is for the MVC controller action or for Web API controller action. Below is a sample code to register Web API action requests. Web. Api. Config. Register(Http. Configuration config). Routes. Map. Http. Route(. name: "Default. Api", route. Template: "api/{controller}/{id}", defaults: new { id = Route. Parameter. Optional }. Creating a Sample Web API Application. Open Visual Studio 2. ASP. Net MVC 4 Web API application and name it Web. Api. Demo. Now go and delete the default Values. Controller. cs and add the Model named Car. Following is the Car. Web. Api. Demo. Models. Car. publicint Registration. Number { get; set; } publicstring Company { get; set; } publicstring Color { get; set; } publicstring Type { get; set; }. Create the Api. Controller class for the Web API. Right click on the Controller folder and select Add Controller. Make sure that the template selected is Empty API Controller. Add the Get and Post methods. Web. Api. Demo. Controllers. Car. Controller : Api. Controller. List< Car> m_Car = new. List< Car> (). Car(). Registration. Number = 1. 00. 0. Company = "Ferrari", Color = "Red", Type = "Sport". Car(). Registration. Number = 1. 00. 1. Company = "Mercedes", Color = "White", Type = "Comfort". Car(). Registration. Number = 1. 00. 2. Company = "Audi", Color = "Black", Type = "Luxury". Http. Response. Message Get. Car(int registration. Number). var car = from c in m_Car where c. Registration. Number == registration. Number select c. Request. Create. Response< Car> (Http. Status. Code. OK, car. First. Or. Default()). Http. Response. Message Post. Car(Car car). m_Car. Add(car). return Request. Create. Response< Car> (Http. Status. Code. Created, car). In the above code, method Get. Car fetches a car based on the registration number and Post. Car adds a new car to the repository. Consume in a Web Application Through JQuery. Create a web application, may be a MVC application itself and in the . Ajax request to add a car.< header> < scriptsrc="~/Scripts/jquery- 1. Add. Carr"). click(function () {. Add. Car(). $. ajax({. POST", data. Type: "json". data: JSON. Type: "application/json; charset=utf- 8", success: function (data) { alert('Car is added successfully!'). An error occured while adding a car!'). Add. Car() { var car = {. Registration. Number: 1. Company: "BMW", Color: "Blue", Type: "Sport". Consume in a Windows Forms Application. In order to consume from the Windows Forms application or from a C# code behind file then the Http. Client class can be used. Following is the C# code to fetch the data from the Web API that we created earlier. Get. Cars(). using (var client = new. Http. Client()). using (var response = await client. Get. Async("http: //localhost: 5. Is. Success. Status. Code). {. var car. Json. String = await response. Content. Read. As. String. Async(). }. Hope this article was informative.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |