Click here to Skip to main content
15,886,422 members
Articles / Web Development / ASP.NET
Tip/Trick

WSDL Equivalent for Rest API

Rate me:
Please Sign up or sign in to vote.
4.76/5 (6 votes)
3 Dec 2015CPOL2 min read 41.8K   12   8
Generate User Interface for Web API that lists down all the http services without writing a single line of code

Introduction

One feature from legacy web services that we would miss in this modern http services days is WSDL. It is the interface to client that:

  1. Lists down all the HTTP operations that can be performed on a Service
  2. Describes input and output parameter data types to invoke the operations

Though, Rest API services are very efficient, it is not self described. Client applications that consume the rest service have no clue about input and output data format unless explicit documentation is provided.

Swagger

Swagger comes to the rescue here. It provides a nice UI listing all the http endpoints and with sample of input and output parameters.

Official site - swagger.io

Let's show you the steps.

  1. Create a Web API project in VS. Create a new controller named 'Customer' and paste the below code.
    C#
     public class CustomerController : ApiController
        {
            static List<Customer> Customers = new List<Customer>();
            static int counter = 0;
    
            // GET api/customer
            public IEnumerable<Customer> Get()
            {
                return Customers;
            }
    
            // GET api/customer/5
            public Customer Get(int id)
            {
                return Customers.Where(c => c.Id == id).FirstOrDefault();
            }
    
            // POST api/customer
            public void Post([FromBody]string value)
            {
                Customer c = new Customer() { Id = ++counter, Name = value };
                Customers.Add(c);
            }
    
            // PUT api/customer/5
            public void Put(int id, [FromBody]string value)
            {
                Customers.Where(c => c.Id == id).Select(c => { c.Name = value; return c; }).ToList();
            }
    
            // DELETE api/customer/5
            public void Delete(int id)
            {
                Customers.Remove(Customers.Where(c => c.Id == id).FirstOrDefault());            
            }
    }
  2. Install Swagger through Nuget package manager console:
    C#
    PM> Install-Package Swashbuckle

    FYI. This installation will create a file - SwaggerConfig under App_Start.

    config

  3. Run the project and type swagger at end of the url in browser.
    C#
    http://localhost:59617/swagger

    You should be seeing this screen - Swagger UI. Click on 'expand operations' to view all the http operations in a detailed way.

    ui

What this Swagger UI is Capable Of ?

  1. All type of Http requests - GET, POST, PUT and DELETE can be triggered from this UI. Input proper data and click 'Try it now' button. Response will be shown right below. Something like Postman in Chrome, Fiddler.
  2. Model Schema - Output model structure which supports both XML and JSON format.
  3. Response content type is editable.
  4. Response includes a lot of information like response data, code and headers.
  5. Shows cURL commands for the requests fired. cURL is a command line utility to create http requests using commands that support all features that can done through browser. First, cURL needs to be installed to use this.

What are you waiting for? Download the attached project and have fun!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Microsoft Global Delivery
India India
Yet another software developer who works on various Microsoft and Web technologies but curious to try something beyond an usual

Comments and Discussions

 
QuestionGood intro to swagger UI Pin
Sambantham.K26-Dec-19 4:29
Sambantham.K26-Dec-19 4:29 
QuestionGood stuff Pin
Member 125966476-Sep-16 0:16
Member 125966476-Sep-16 0:16 
QuestionWhat about an existing ApiExplorer? Pin
Easked3-Dec-15 22:12
Easked3-Dec-15 22:12 
AnswerRe: What about an existing ApiExplorer? Pin
William E. Kempf7-Dec-15 3:17
William E. Kempf7-Dec-15 3:17 
GeneralRe: What about an existing ApiExplorer? Pin
Easked8-Dec-15 22:12
Easked8-Dec-15 22:12 
QuestionThis is cool thing for developer to troubleshoot Pin
santosh poojari3-Dec-15 18:50
santosh poojari3-Dec-15 18:50 
Good start !Are there options to switch them off for production environment?
Happy Coding
"San"




SuggestionYou've got some broken links Pin
Paul Tait3-Dec-15 15:06
Paul Tait3-Dec-15 15:06 
GeneralRe: You've got some broken links Pin
Kanivalan Raju3-Dec-15 17:31
Kanivalan Raju3-Dec-15 17:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.