Getting an unhandled exception error when trying to call an api from an ASP.Net Core web app

I have an ASP.Net Core 2.0 web application that calls a Web API in a different project. When I try to run it I get the error An unhandled exception occurred while processing the request. Further detail leads to this: WinHttpException: A connection with the server could not be established. I have not run into this yet. The code for the controller is below. It erros out on this line:
HttpResponseMessage Res = await client.GetAsync("api/players");

Open in new window


Here is my HomeController.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MyAPIClient.Models;
using Newtonsoft.Json;

namespace MyAPIClient.Controllers
{
    public class HomeController : Controller
    {
        string Baseurl = "http://localhost:60000/";
        public async Task<ActionResult> Index()
        {
            List<Player> PlayerInfo = new List<Player>();

            using (var client = new HttpClient())
            {
                //Passing service base url  
                client.BaseAddress = new Uri(Baseurl);

                client.DefaultRequestHeaders.Clear();
                //Define request data format  
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                //Sending request to find web api REST service resource GetAllPlayers using HttpClient  
                HttpResponseMessage Res = await client.GetAsync("api/players");

                //Checking the response is successful or not which is sent using HttpClient  
                if (Res.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api   
                    var playerResponse = Res.Content.ReadAsStringAsync().Result;

                    //Deserializing the response recieved from web api and storing into the Employee list  
                    PlayerInfo = JsonConvert.DeserializeObject<List<Player>>(playerResponse);

                }
                //returning the employee list to view  
                return View(PlayerInfo);
            }
        }

        [HttpPost]
        public async Task<ActionResult> Search(string searchterm)
        {
            List<Player> PlayerInfo = new List<Player>();
            using (var client = new HttpClient())
            {
                //Passing service base url  
                client.BaseAddress = new Uri(Baseurl);

                client.DefaultRequestHeaders.Clear();
                //Define request data format  
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                //Sending request to find web api REST service resource GetAllPlayers using HttpClient  
                HttpResponseMessage Res = await client.GetAsync("api/players/find/" + searchterm);

                //Checking the response is successful or not which is sent using HttpClient  
                if (Res.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api   
                    var playerResponse = Res.Content.ReadAsStringAsync().Result;

                    //Deserializing the response recieved from web api and storing into the Employee list  
                    PlayerInfo = JsonConvert.DeserializeObject<List<Player>>(playerResponse);

                }
                //returning the employee list to view  
                return View(PlayerInfo);
            }
        }

        public async Task<ActionResult> Search()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Edit(int StudentId)
        {
            List<Player> PlayerInfo = new List<Player>();

            //Get the student from studentList sample collection for demo purpose.
            //You can get the student from the database in the real application
            var std = PlayerInfo.Where(s => s.ID == StudentId).FirstOrDefault();

            return View(std);
        }
    }
}

Open in new window

Any help is appreciated.
dodgerfanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
not really in .NET Core but have you tried to catch the exception's InnerException for the details?

Exception.InnerException Property
https://msdn.microsoft.com/en-us/library/system.exception.innerexception%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

there are many possible reasons why you getting error: A connection with the server could not be established
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
webapi

From novice to tech pro — start learning today.