Click here to Skip to main content
15,916,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am new to asp.net core 7.0.

I am working on the web api. However I created one controller with [ApiController] decoration. In the launchSettings.json file it shows port number as below :-

"iisSettings": {
  "windowsAuthentication": false,
  "anonymousAuthentication": true,
  "iisExpress": {
    "applicationUrl": "http://localhost:14023",
    "sslPort": 44381
  }

Means that my Web API will be pointing to http://localhost:14023[^]

However when I run the application and navigate to http://localhost:14023[^]
it shows below error :
Quote:
This site can’t be reached
localhost refused to connect.
Quote:
ERR_CONNECTION_REFUSED



And when I hit the same API URL through Postman then below error coming :-

Quote:
Error: connect ECONNREFUSED 127.0.0.1:
Quote:
14023



Can anyone please help on this.

Initially I thought that it could be related to CORS so I added below code in Program.cs file :-

builder.Services.AddCors();

app.UseCors(options =>
options.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);


I am stuck on this.

What I have tried:

I tried adding the CORS in Program.cs class as below :-

<pre>
builder.Services.AddCors();

app.UseCors(options =>
options.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
Posted
Updated 16-Feb-23 1:26am
Comments
Richard Deeming 16-Feb-23 6:26am    
Have you tried connecting on port 44381 instead? Eg: https://localhost:44381/
Member 11052432 16-Feb-23 6:30am    
What does this URL refer?

My Web API URL is http://localhost:14023. I am hitting this URL directly on Browser and Postman.
Richard Deeming 16-Feb-23 6:33am    
Your settings specify "sslPort": 44381. If the API is configured to require HTTPS, that's the port you'd need to use; HTTP connections on port 14023 would be rejected.
Member 11052432 16-Feb-23 6:41am    
I also tried this as well. Still same issue.

https://localhost:44381/api/member

1 solution

ASP.NET Core projects are configured to bind to a random HTTP port between 5000-5300 and a random HTTPS port between 7000-7300. This default configuration is specified in the generated
properties/launchSettings.json file and can be overridden. More on this can be found - Configure endpoints for the ASP.NET Core web server[^]

Visual Studio is overriding your port. You can change VS port editing this file Properties\launchSettings.json or else set it by code:

var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseUrls("http://localhost:80") // <-----
            .Build();

        host.Run();


Read this solution here - How to change the port number for Asp.Net core app[^]
 
Share this answer
 
Comments
Member 11052432 16-Feb-23 9:14am    
Will above port 80 will work for API?

Means If I hit url : localhost:80/api/member from browser or postman.

Will it work?
Andre Oosthuizen 16-Feb-23 9:31am    
As above, if it is http, use 5000-5300, if it is https, use 7000-7300.

I did not test the code, it is based on a solution elsewhere, which is why I gave the link as well, read up some more on the links supplied.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900