I have a Visual Studio solution with two projects; API and MVC. MVC is my MVC-project containing my views etc., API is my API-project containing the API-endpoints and logic. Since I have two projects, I have configured my solution to have multiple startup-projects.
HttpClient
public const string AuctionAPI = "http://localhost:44337/";
public const string AuctionClient = "http://localhost:44398/";
public static HttpClient GetClient()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(AuctionAPI);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
MVC Controller (Notice: I receive the error on line 6, when I set HttpResponseMessage and try to call client.GetAsync()
[Route("item/{itemNumber}")]
public async Task<IActionResult> IndexForItem(int itemNumber)
{
var client = GetClient();
//ERROR: In next line, HttpResponseMessage it tries to call my API controller and fails
HttpResponseMessage response = await client.GetAsync($@"api/Auction/SeeOneItem/{itemNumber}");
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
var model = await response.Content.ReadAsAsync<AuctionItemProxy>();
return View(model);
}
else
{
return Content("An error occured.");
}
}
API Controller (Notice: my breakpoint in this method never hits)
[HttpGet("SeeOneItem/{itemNumber:int}")]
public AuctionItem GetAuctionItem(int itemNumber)
{
var item = _auctionItemDataFactory.AuctionItems.First(x => x.ItemNumber == itemNumber);
if (item == null)
{
return null;
}
return _auctionItemDataFactory.AuctionItems.First(x => x.ItemNumber == itemNumber);
}
Actual error I receive
IOException: Unable to read data from the transport connection