How To Create Restful Service In C#
Groundwork
Today's application need to be communicated on all cross platforms to fulfil the need of today'southward mod application, to fulfil these requirement lots of technologies are invented and they are used every bit per requirement. The Web API is one of the latest technology to fulfil the requirement of enterprise application.
Allow's larn nearly it footstep by step:
What is ASP.Net Spider web API
The ASP.NET Web API is one of the Microsoft open source engineering to build the powerful Residual Services which volition communicate across all platform and devices over HTTP.
Allow united states implement it practically past creating 1 sample application.
Step one
Create ASP.NET Web API Service
- "Commencement", and then "All Programs" and select "Microsoft Visual Studio 2015".
- "File", and so "New" and click "Project", then select "ASP.NET Web Application Template" and provide the Project a name equally y'all wish and click on OK, then the following windows volition appear; select empty template and check on Web API as in the following image.
- Choose MVC empty application option and click OK.
- Now later calculation the MVC project, the Solution Explorer will await similar the following:
After creating the ASP.Net Spider web API project, the preceding folder construction and files are added into the project by default which are same as MVC projection, so let us learn about them in brief as in the following:
- App_Start folderThis folder contains the application configuration details such as routing, authentication, filtering of URL and so on.
- ControllerThis binder contains the controller and their methods. The controller is responsible for processing the user request and render output equally a view.
- ModelsThis folder contains the entities or properties used to shop the input values.
Step 2
Create Model Class.
Right click on Model folder in the created Web API awarding, give the grade name EmpModel or as yous wish and click on OK.

At present write the following code into the EmpModel Class.
EmpModel.cs
- public class EmpModel
- {
- public cord Proper name { get ; set ; }
- public string Metropolis { get ; ready ; }
- }
Step 3
Add Spider web API Controller.
Right click on Controller folder in the created Web API awarding and Spider web API 2 Empty controller as in the post-obit,

Now click on add together and requite the form proper name Home or as y'all wish and click OK as:

Now we accept added Model grade and spider web API controller grade. Permit's create the method into the HomeController form as in the following,
HomeController.cs
- using Organization;
- using Organisation.Spider web.Http;
- namespace CreatingWebAPIService.Controllers
- {
- public class HomeController : ApiController
- {
- [HttpPost]
- public bool AddEmpDetails()
- {
- return true ;
- }
- [HttpGet]
- public string GetEmpDetails()
- {
- return "Vithal Wadje" ;
- }
- [HttpDelete]
- public cord DeleteEmpDetails( string id)
- {
- render "Employee details deleted having Id " + id;
- }
- [HttpPut]
- public string UpdateEmpDetails( cord Proper noun, Cord Id)
- {
- render "Employee details Updated with Proper noun " + Name + " and Id " + Id;
- }
- }
- }
In this in a higher place code we have created 4 methods for Insert, Update, Display and delete the records .The above web API controller methods defined with HTTP methods which are the following,
- GET: Become the resources (Records) from particular source such every bit SQL database.
- POST: Used to insert the records into the item source such every bit SQL, Oracle database.
- PUT: Used to modify the resource or records.
- DELETE : Used to delete the specific resource or record from particular source.
Step 4: Run the awarding.
Now everything is gear up lets run the application, it will throw the post-obit error:

The reason of above error is we are not post-obit the url construction which are set into the routing of web API awarding. The post-obit is the default URL structure of ASP.NET Web API defined into the WebApiConfig.cs file.

From the higher up routing construction, we tin access the resource using url format i.e Base url+api+apicontroller. Unlike MVC controller the web controller by default access all the methods just using controller proper noun. Now our service is fix. Permit's test it using REST client browser extension because browser just supports Get request directly from URL non others .
Our Balance Service URL will be http://localhost:56290/api/Home which is described as follows,
- http://localhost:56290: Is the base address of spider web API service, It tin can be dissimilar as per your server.
- api: It is the used to differentiate between Web API controller and MVC controller request .
- Dwelling: This is the Web API controller proper name.
Step 5
Testing Web API REST service using REST client
Now let us test the application using Remainder customer every bit:
POST

In the above prototype Web API Rest Service, HTTP Postal service method returns the 200 Condition lawmaking ways Rest service is successfully executed.
GET Method

In the above paradigm Web API REST Service, HTTP GET method returns the 200 Status code means REST service is successfully executed and render the XML format output.
Note
- The Web API REST service by default return the output every bit per browser default message header such as XML or JSON.
The higher up XML output return in Firefox browser., Now let's change the browser as IE, and then output will return in JSON format every bit in the following,

From the above output its clear that output format by default depend on browser message format header.
Delete Method

In the above image Web API Balance Service, HTTP Delete method returns the 200 Status code means REST service is successfully executed and return the XML format output. I hope from the preceding examples we learned how to create Spider web API REST Service.
Note
- Download the Zip file of the sample awarding for a meliorate understanding.
- In my next article, Introduction ASP.Internet Web API nosotros will learn what is Web API Residuum Service, When and where to use Web API Rest Service.
Summary
I promise this commodity is useful for all readers. If y'all have whatsoever suggestions, then please mention it in the annotate department.
How To Create Restful Service In C#,
Source: https://www.c-sharpcorner.com/UploadFile/0c1bb2/creating-Asp-Net-web-api-rest-service/
Posted by: klugedescear.blogspot.com

0 Response to "How To Create Restful Service In C#"
Post a Comment