Creating A REST Webservice With C# And Visual Studio

In this tutorial Creating A REST Webservice I guide you through the process of creating a RESTful Webservice with C# and Visual Studio.

CLICK HERE to go to the updated version for creating a REST Webservice for .NET Core.

For this demo I am using Visual Studio 2017 with the ASP.NET and web development package installed.

Visual Studio ASP.NET & Web - Creating A REST Webservice
Visual Studio ASP.NET & Web

Within Visual Studio we create a new ASP.NET Web Application Project by navigation to “File ->  New -> Project” and selecting “ASP.NET Web Application (.NET Framework)”.


New Project ASP.NET Web Application - Creating A REST Webservice
New Project ASP.NET Web Application

Close ×

On the next screen it is important to choose the “Web API” template. This template automatically creates a skeleton of an rest interface.

Web API Template - Creating A REST Webservice
Web API Template

 

After the creation of the project structure we hit run to see what Visual Studio initially provides us.

ASP.NET Web API initial look - Creating A REST Webservice
ASP.NET Web API initial look

By navigation to API we have access to the template REST methods and their documentation, which are created by default from Visual Studio.

Initial REST methods and description - Creating A REST Webservice
Initial REST methods and description

Allrigt. If you now type in “http://localhost:53975/api/values” (the port may vary on your local environment) in the browser, you’ll get a return value of type json.

This REST methods and their return values are defined in the ValuesController within the Controller folder of your Web API project. There you find all your methods like Get, Post, Put, Delete etc.

Close ×

If you want to change the URL  you have to make amendments within the WebApiConfig.cs file in the App_Start Folder.

With this code every API call have to be structured with “api/{name of your Controller}/{id}.

By adding a new Controller you can choose between various templates. In this demo I’ll use the Web API 2 Controller with read/write actions.

Add New Controller (Read/Write) - Creating A REST Webservice
Add New Controller (Read/Write)

The name of the new controller is “ConfigController”.

After starting the application by pressing F5 we now have an additional REST interface by calling the url “http://localhost:53975/api/config”, which references to the ConfigController by its name.

Config REST - Get result - Creating A REST Webservice
Config REST – Get result

 

And that’s basically it. You can now change the interfaces to your naming convention, add logic to the different methods and return whatever data you want.

 

Video Tutorial on YouTube:



Close ×

 

Happy coding :-).

30 Replies to “Creating A REST Webservice With C# And Visual Studio”

  1. In the first step “Choosing ASP.NET Template” you should NOT pick “WebApi”, because it will include a ton of crap you don’t need (jquery, bootstrap, fonts, …). To get a nice clean REST API, you should choose ASP.NET – Empty Project. After that you right click the project and “Add / New Scaffolded Item…” and select a Web API 2 Controller.

    1. Hi John, you are right – the template includes a lot of things that you probably won’t need when you are going for a straight rest-only project. I will shortly post an updated version where I will cover the creation of a .Net Core Rest Service which will be as plain as it gets.

  2. I love your website and all of the really great tutorials you have on here. Thanks so much. You seem like a really smart and tech-savvy guy – the type of person I’m striving to be. Thanks so much!!

    1. Hi Alex, thank you for your kind words! To help people and to encourage them to do something in tech is exactly the reason why I made this site.

    1. Hi Michael,

      how do you access the api? Usually you have to set the content-type header to application/json if the default value differs.

  3. Thanks Patrick for this very simple and easy tutorial.

    I want to ask you how can I get the json array from the result and post it in a listview in Android Studio ?

    Thanks.

    1. Hi James,

      I didn’t work with Android Studio in a while, but you have to look at a Http Client (or something which can read a rest endpoint) and extract the array from the response.

  4. Hi,
    do you now why in the end it return me XML and not JSON when I put value for id, and how to make that return me JSON?
    thanks

  5. All of those who have problems with XML instead of Json when using VS: Try to add

    “var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == “application/xml”);
    config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);”

    at the end of the Register method in WebApiConfig.cs (in my VS version it is in App_Start).

  6. Do yoᥙ mind if I quote a few of your ⲣostѕ aѕ long as I provide credit and sources back to your
    website? My blog is in the exact same niche as yours аnd mү users would genuinely Ƅenefit frοm some of the information you present here.
    Please let me кnow if this alright ᴡith you.
    Many thanks!

  7. For those who want to display the results in JSON format instead of XML, I added this on WebApiConfig.cs :

    config.Formatters.Remove(config.Formatters.XmlFormatter);

    So my register function looks like this:

    public static void Register(HttpConfiguration config)
    {
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
    name: “DefaultApi”,
    routeTemplate: “api/{controller}/{id}”,
    defaults: new { id = RouteParameter.Optional }
    );
    config.Formatters.Remove(config.Formatters.XmlFormatter);
    }

    Basically what I read is that it remover the XML format and returns it to the default format that is JSON.

  8. I have created REST Webservice and I want to run it on another computer with Windows
    How do I do that ?
    When running project from Visual Studio console app is launched and it says
    Now listening on: https://localhost:5001
    How can I copy Webservice to another computer and install it there so it listens for port 5001 there
    Visual Studio generates dll and json files
    What should I do with them ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.