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

Support Desk Day 2 of n

Rate me:
Please Sign up or sign in to vote.
4.75/5 (8 votes)
30 Dec 2015CPOL3 min read 15.4K   10   1
This is day two of a multi day project using ASP.NET Web API for the server and WPF MVVM for the client.

Introduction

For Support Desk, I used Visual Studio 2015 ASP.NET Web API to host a help desk server. It will allow authenticated users to create, edit, and search support tickets, customers, and solutions. The client is in WPF, but any client using HTTP can access the server through Web API allowing the customer to easily customize their client.

Day One

On day one, we started a new solution containing an ASP.NET Web Application project. We examined the "Controllers" and "Models" folders. Then, we made minor changes to the code behind so we can do a simple proof of concept exchange with the client that we can expand on later. You can find the day one article here http://www.codeproject.com/Tips/1059338/Support-Desk-Day-of-n and download the completed project from CodePlex.com.

Day Two

On day two, we will open a separate Visual Studio solution for our WPF client. We'll make changes to the code behind for the MainWindow by adding a single method. Our method will do a Get request on our ValuesController "api/values/" which will return our string "Hello World!" from the server.

Download the completed day two source code from CodePlex.

Create the Client

Open a second copy of Visual Studio. Then File>>New>>Project. Select Visual C# then WPF Application. Set the Name to "Support Desk Client". Click OK.

Image 1

In Solution Explorer, right click on the project and select "Manage NuGet Packages".

Image 2

Install the Newtonsoft.Json NuGet package. We need this package to use "JsonConvert" to Serialize and Deserialize our objects returned from the server.

Change the code in the MainWindow.xaml.cs file to the following:

C#
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Windows;

namespace Support_Desk_Client
{
    public partial class MainWindow : Window
    {        
        public MainWindow()
        {
            InitializeComponent();
            GetRunAsync().Wait();
            
        }
        async Task GetRunAsync()
        {
            using (var client = new HttpClient())
            {
                string StringResponce;
                client.BaseAddress = new Uri("http://localhost:57975/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add
                        (new MediaTypeWithQualityHeaderValue("application/json"));

                var response = client.GetAsync("api/Values/").Result;
                if (response.IsSuccessStatusCode)
                {
                    var GetString = await response.Content.ReadAsStringAsync();
                    StringResponce = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(GetString);
                    if (StringResponce != null)
                    {
                        MessageBox.Show(StringResponce);
                    }

                }

            }
        }
    }
}

GetRunAsync() calls are method from the constructor as MainWindow is loading. The Wait() method signifies for the compiler to wait for our GetRunAsync() method to complete. Communication with the server could be delayed or disrupted so declare GetRunAsync() as async. The whole exchange is wrapped in a using statement to ensure the connection to the server will be closed when we've finished.

Next, we declare a new instance of HttpClient, client. Then set its BaseAddress to the Project Url from Support Desk day 1 "http://localhost:57975/". The next two lines are to request the json format from the server. Then declare response equal to the result of our get call to the server GetAsync("api/Values/"). If we go to the server project and look at the ValuesController will see that a Get call returns the simple string "Hello World!"

C#
// GET api/values
        public string Get()
        {
            return "Hello World!";
        }

If the response is successful, the var GetString is set equal to the Content of response read as a string and then deserialized as object type string.

C#
StringResponce = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(GetString)

If HttpClient or MediaTypeWithQualityHeaderValue has red squiggly lines under, then you left out the using statements for System.Net.Http and System.Net.Http.Headers. Newtonsoft.Json.JsonConvert requires the Newtonsoft.Json NuGet package.

Passing a simple string back and forth doesn't provide much functionality to our program, but later will pass our own custom objects. Now go back to the server project and run it. Give it enough time to load and come back to our client project and compile and run, and as you can see, we had a successful exchange with the server.

Image 3

That's it for day two. Over the next few days, we will add the support tickets and customers to the server and use a bearer token on our client for authentication. http://www.codeproject.com/Tips/1065769/Support-Desk-Day-of-n.

License

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


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionMissing Image Pin
pharry2221-Dec-15 9:17
pharry2221-Dec-15 9:17 

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.