James213
James213
CC#
Created by James213 on 9/2/2023 in #help
✅ Frontend unable to connect to webapi when starting server manually (dotnet run)
When I run my C# Webapi project through Visual Studio Code debugger (or VS) then my reactapp can make a GET request to the webapi, however if I start up the webapi via cmd dotnet run and make a GET request I get net::ERR_CONNECTION_REFUSED This is the output from VS Code when I start the webapi through the debugger.
* Executing task: C:\Program Files\dotnet\dotnet.exe build C:\Users\Username\source\repos\Username\InsuranceCalculator/InsuranceCalculator.sln /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary

MSBuild version 17.6.1+8ffc3fe3d for .NET
Determining projects to restore...
All projects are up-to-date for restore.
webapi -> C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi\bin\Debug\net7.0\webapi.dll
* Terminal will be reused by tasks, press any key to close it.
* Executing task: C:\Program Files\dotnet\dotnet.exe build C:\Users\Username\source\repos\Username\InsuranceCalculator/InsuranceCalculator.sln /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary

MSBuild version 17.6.1+8ffc3fe3d for .NET
Determining projects to restore...
All projects are up-to-date for restore.
webapi -> C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi\bin\Debug\net7.0\webapi.dll
* Terminal will be reused by tasks, press any key to close it.
This is the output from cmd when I start the webapi through the cmd.
C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi>dotnet run
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5206
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi
C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi>dotnet run
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5206
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\Username\source\repos\Username\InsuranceCalculator\webapi
33 replies
CC#
Created by James213 on 6/19/2023 in #help
❔ Unable to make a GET request
I get a 403 - "Forbidden" from this code. I have successfully tested (200) with Postman and a Python version. Here is the doc fyi https://docs.github.com/en/rest/gists/gists?apiVersion=2022-11-28#list-gists-for-the-authenticated-user
// Create an HttpClient object (pass into function)
HttpClient client = new();

...


public async Task<HttpResponseMessage> GetResponse(string apiKey, string url, HttpClient client)
{
/*
Contents
1. Headers for the request
2. Return the response from the API using the HttpClient object
*/

// 1. Headers for the request
Dictionary<string, string> headers = new()
{
{ "Accept", "application/vnd.github+json" },
{ "Authorization", $"Bearer {apiKey}" },
{ "X-GitHub-Api-Version", "2022-11-28" }
};

// 2. Return the response from the API using the HttpClient object
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}

HttpResponseMessage response = await client.SendAsync(request);

return response;

}
// Create an HttpClient object (pass into function)
HttpClient client = new();

...


public async Task<HttpResponseMessage> GetResponse(string apiKey, string url, HttpClient client)
{
/*
Contents
1. Headers for the request
2. Return the response from the API using the HttpClient object
*/

// 1. Headers for the request
Dictionary<string, string> headers = new()
{
{ "Accept", "application/vnd.github+json" },
{ "Authorization", $"Bearer {apiKey}" },
{ "X-GitHub-Api-Version", "2022-11-28" }
};

// 2. Return the response from the API using the HttpClient object
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}

HttpResponseMessage response = await client.SendAsync(request);

return response;

}
7 replies
CC#
Created by James213 on 12/30/2022 in #help
❔ Repository not found - Git
When I go to push, I get the error
`> git pull --tags origin master
remote: Repository not found.
fatal: repository 'https://github.com/James7851/fem-manage-landing-page.git/' not found
`> git pull --tags origin master
remote: Repository not found.
fatal: repository 'https://github.com/James7851/fem-manage-landing-page.git/' not found
Yet when I click the click it goes to the repository. As with the links with git remote -v git config --local --list shows the same email that is associated with the repository, and I am pushing with that GitHub account on Visual Studio Code. Although when I do git log --pretty=full I get a different email for the (HEAD -> master), though for (origin/master) it's the correct email Also, when I change the repository to public on GitHub and then try to push it says "You don't have permissions to push to "James7851/fem-manage-landing-page" on GitHub. Would you like to create a fork and push to it instead?"
9 replies
CC#
Created by James213 on 11/30/2022 in #help
❔ How to pass JS object to function.
How do I pass the JS object to the C# function in that it accepts it as an object of a defined Class? I looked at an example using Ajax (don't know if that has to do with anything). https://www.dotnettricks.com/learn/webapi/how-to-pass-javascript-complex-object-to-aspnet-web-api-and-mvc I'm getting null values. I tried removing JSON.stringify, but no luck.
// JS Object
const formData = { "startDate": startDateInput.value, "endDate": endDateInput.value, "stock": stock.value };

// Send to C# function
const response = await fetch('https://localhost:5681/api/stock/getStockData/' + JSON.stringify(formData));
// JS Object
const formData = { "startDate": startDateInput.value, "endDate": endDateInput.value, "stock": stock.value };

// Send to C# function
const response = await fetch('https://localhost:5681/api/stock/getStockData/' + JSON.stringify(formData));
// C# function
[HttpGet]
[Route("api/stock/getStockData/{formData}")]
public async Task<IActionResult> GetStockDataAsync(Stock formData)
{

}

// Stock Class
public class Stock
{
public string stock { get; set; }
public string? startDate { get; set; }
public string? endDate { get; set; }
}
// C# function
[HttpGet]
[Route("api/stock/getStockData/{formData}")]
public async Task<IActionResult> GetStockDataAsync(Stock formData)
{

}

// Stock Class
public class Stock
{
public string stock { get; set; }
public string? startDate { get; set; }
public string? endDate { get; set; }
}
Thank you
12 replies