119

I have copied my solution file from another system and tried to run it on my machine: For this, I have gone to the project folder and deleted the previous solution file and copied the new one.

Now, it started giving me error. connection refused etc. So, I got one solution from here: How to solve ERR_CONNECTION_REFUSED when trying to connect to localhost running IISExpress - Error 502 (Cannot debug from Visual Studio)?

According to it, I changed the port number from 1049 to 1052. It loaded and one of the pages gets loaded. But when I entered the desired login credentials then it redirected to the older port again, like this:

http://localhost:1049/Home/SearchFlight

and the error is:

 This site can’t be reached

 localhost refused to connect.
 Search Google for localhost 1049 Home Search Flight
 ERR_CONNECTION_REFUSED

Now, I don't know what to do from here. I have changed the port number to 1052 but still its pointing to 1049. What should i do now?

Community
  • 1
  • 1
duke
  • 1,816
  • 2
  • 18
  • 32

41 Answers41

138

I had a similar issue. Try this and it should work

  1. Go to your project folder and open the .vs folder (keep your check hidden item-box checked as this folder may be hidden sometimes)

  2. In the .vs folder open the config folder

  3. See that applicationhost.config file there? Delete that thing. (Do not worry it will regenerate automatically once you recompile the project.)

SharpC
  • 6,974
  • 4
  • 45
  • 40
Kush
  • 1,397
  • 1
  • 8
  • 3
88

Usually on local machine we are getting errors like This site can't be reached localhost refused to connect because we have self signed certificate configuration broken for local IIS Express. Sometimes it happens when you change URLs in launchSettings.json or never configured self signed certificates on this machine at all.

To fix self-signed certificate on the local machine you need to: 

  1. Delete .vs folder (requires to close Visual Studio 2017)
  2. Run these commands in cmd As Administrator:

    cd "C:\Program Files (x86)\IIS Express"

    IisExpressAdminCmd.exe setupsslUrl -url:https://localhost:12345/ -UseSelfSigned

  3. Make sure you run VS2017 As Administrator and check if the issue addressed

Note: https://localhost:12345/ is what is in your launchSettings.json for HTTPS

Natrium
  • 30,772
  • 17
  • 59
  • 73
Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
  • 4
    this actually helped. I had combined issue: Fiddler running and certificate missing. – Diomos Oct 15 '18 at 10:20
  • 2
    This was the winner for me. I had added an openssl cert for a different app on a different port on localhost, and it hosed the self-signed cert I was using for iis express. Repairing the self-signed cert on the specific localhost port fixed it. – Dr. Hilarius Mar 27 '19 at 16:25
  • 2
    Fixed, thank you so much! I've seen a lot of people saying delete .vs folder, but this is the first answer I've seen about the ssl cert, that was the clincher for me. – tagustini Jun 20 '19 at 08:22
  • 1
    in my case i had to run on different port, on 44343. IisExpressAdminCmd.exe setupsslUrl -url:https://localhost:44343/ -UseSelfSigned – Vitaliy Markitanov Sep 26 '19 at 19:28
  • 2
    My application was working fine until recently, started getting "this site cant be reached" messages in browser.... this solved my issue – useruser00 Jan 05 '21 at 10:06
  • 1
    Thanks champ this actually worked thanks a lot – Dylan Karimagoko Oct 24 '21 at 10:55
43

Changing port number has resolved the issue

enter image description here

vinayak hegde
  • 2,117
  • 26
  • 26
  • That is solved momentarily. I get bored changing the port every time I run the project. – jstuardo Oct 29 '18 at 23:07
  • Using VS 2019 Preview 2. Got the same error, and fixed it by changing to the Project URL above. (goodness knows why) :-) – netchicken Jan 28 '19 at 22:40
  • I actually had to delete the whole .vs folder and change the port number, but at least eventually it worked – William Nov 08 '19 at 21:12
  • @jstuardo interesting, mine was solved permanently. I don't have to change it every time. – corix010 Nov 09 '21 at 18:55
  • In my case I removed the port number completely from the project url, created a new virtual directory and it works now. ((shrug)) – Jamie M. Dec 22 '22 at 13:49
18

Unfortunately, none of these answers worked for me. This answer finally did for Chrome only. Unfortunately, you have to do it each time:

  1. Open this in your browser: chrome://net-internals/#hsts
  2. Go to Delete domain security policies
  3. Enter "localhost"
  4. Restart browser

Google Chrome redirecting localhost to https

Shane
  • 4,185
  • 8
  • 47
  • 64
  • Wow, I've been trying to fix this issue with trying to debug in VSCode the last hour and a half, this finally did it. – Nard Dog Oct 27 '20 at 20:08
  • 1
    None of the above and other sites solved my problem but this one did! – E-A Jul 27 '21 at 08:46
17

Just Delete the (obj)Object folder in project folder then Run the application then It will work fine. Steps:

  1. Right Click on the project folder then select "Open folder in file Explorer".
  2. Then Find the Obj folder and Delete that.
  3. Run the Application.
12

I had the same problem. Deleting .vs folder (including all contents, folder might be hidden) worked for me.

Baqer Naqvi
  • 6,011
  • 3
  • 50
  • 68
  • When we copy whole project to another PC so the project also handle our own .vs config so delete that hidden folder, reopen the project in new PC, it will be working. – Osify Nov 02 '18 at 05:04
10

I solved a similar problem by listing bound IP addresses in a cmd window running as admin:

netsh http show iplisten

Then, one by one, blowing them all away:

netsh http delete iplisten ipaddress=127.0.0.200
netsh http delete iplisten ipaddress=127.0.0.2
...
Watusimoto
  • 1,773
  • 1
  • 23
  • 38
  • At one point in time I added a url reserveration for `http://*:44300/` and this is what was causing localhost to not connect for me. I ran a similar command as you, `netsh http delete urlacl http://*:44300/` and localhost connects and debugs as expected now! – Quiver Jan 29 '20 at 16:07
8

In my case, I need to update "publishAllPorts" = false in .Net 5 MVC project's launchSettings.json.

 "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
      "publishAllPorts": false
    }
ChinaHelloWorld
  • 1,007
  • 1
  • 12
  • 6
  • this question tagged for iis, but your answer is solved my problem for me after trying a lot of useless things. thank you. to other know, this is for docker launcher. – Mohammadreza Askari Dec 20 '21 at 07:16
  • 1
    This is works, description on Docker's document - "publishAllPorts" : Allocates an ephemeral host port for all of a container’s exposed ports, it seems it conflict with specific port declare by ASP.NET core project. – Cheung Jun 20 '22 at 03:28
6

I ran my Visual Studio as "Run as administrator" and this solved my problem.

anu
  • 305
  • 1
  • 5
  • 14
  • I'd wager that this is the case 90% of the time. Note that when you install a VS update and click "Launch" at the end of installation, it will NOT run as administrator. Gets me every time. – moomoo Mar 06 '19 at 20:02
6

Go to control panel > Programs and feature > IISExpress > Repair

Ron
  • 1,901
  • 4
  • 19
  • 38
4

If found it was because I had been messing about with netsh http add commands to get IIS Express visible remotely.

The symptoms were:

  • the site would fail to connect in a browser
  • the site would fail to start (right click IIS Express in the Taskbar and it was missing)
  • if I ran Visual Studio as admin it would start in IIS Express and be visible in the browser

The solution was to list all bound sites:

netsh http show urlacl

then delete the one I had recently added:

netsh http delete urlacl url=http://*:54321/


In addition I had removed localhost from the hidden solution folder file .vs\config\application.config for the site:

<bindings>
    <binding protocol="http" bindingInformation="*:54321:" />
</bindings>

so had to put that back:

<bindings>
    <binding protocol="http" bindingInformation="*:54321:localhost" />
</bindings>
SharpC
  • 6,974
  • 4
  • 45
  • 40
  • Fantastic answer. In my case, I was trying to get the site to be remotely accessed using **ngrok** for that purpose. It didn't work for me and I left it unaccessible for that particular port, forgetting totally that during the proccess I eventually used ```netsh http add```. Thank you very much! – oneberenjena Sep 10 '20 at 14:27
3

Open cmd as administrator and run this command:

netsh http delete iplisten ipaddress=xxx.xxx.x.xxx

ipaddress is your private ip.
This worked for me.

dferenc
  • 7,918
  • 12
  • 41
  • 49
Hamza Shahid Ali
  • 224
  • 4
  • 14
3

This happened to me in VS2017 v 15.6.7 using .net core 2.0.

When the project first launched it created an SSL Cert for localhost and started fine. When I disabled SSL I kept getting this issue.

Apparently when you disable SSL the port numbers are changed and not all of the port numbers get changed to the new port. Make sure port numbers on Debug are all the same number. enter image description here

Donny V.
  • 22,248
  • 13
  • 65
  • 79
3

I was having this issue and solved it by closing all open instances of Visual Studio.

R. Salisbury
  • 1,954
  • 16
  • 17
  • 1
    Also possible two instances of Visual Studio with different projects are running on the same port. Making the ports different worked for me. – Ben Aug 24 '20 at 19:12
2

I faced this annoying problem all the time in my Visual Studio MVC application especially when you start your work in the morning. Many solutions in this thread works sometimes; but there were many times none of this solves my issue.

At last I figured out only a combination of these steps works for me. Hope this helps somebody.

  1. Close VS.

  2. Delete .vs folder under your solution. Don't worry, it will be automatically created when you open the solution again.

  3. Delete bin and obj folders.

  4. Delete your cookies from the browser that you use to debug the application.

  5. Delete the *.csproj.user file located in the project folder.

  6. Open your startup project file (*.csproj) in Notepad++ or your favorite editor. Go towards the end of the file and delete below 3 lines that contains the local development port info. Don't worry it will be recreated on running the solution again.

      <DevelopmentServerPort>0</DevelopmentServerPort>
      <DevelopmentServerVPath>/</DevelopmentServerVPath>
      <IISUrl>http://localhost:60573/</IISUrl>
    
  7. Open your solution again, clean, build and run it. You should be good to go. If this don't work, restart your machine after step 6.

Renji
  • 123
  • 8
1

For this to work for me, all I had to do was recreate the virtual directory. I had re-installed Windows and then pulled the project back out of Git. To do this, right-click on the solution project and choose Properties, then go to the Web tab and click the Create Virtual Directory beside the Project Url.

I hope this helps someone else too!

Bud

1

Project properties> Web > Create Virtual Directory worked for me

cagedwhale
  • 51
  • 4
1

Right Click on Project >> Properties >> Select Web Tab >> Under server Select "Use Visual development server" so indirectly it will uncheck "Local IIS Server"

Hope so it may resolve this issue.

1

Change the port number and remove script debugging (if enabled) to resolve in localhost.

smonff
  • 3,399
  • 3
  • 36
  • 46
1

I've tried everything but nothing worked, but this did. If you can debug on local IIS instead of Express, change the configurations as shown in the image below. Do not forget to click on "Create virtual directory"

enter image description here

Alejandro Bastidas
  • 1,452
  • 2
  • 20
  • 26
1

rebooting the machine was the only thing that worked for me

Fabio Napodano
  • 1,210
  • 1
  • 16
  • 17
  • 1
    I got a popup window saying something about a problem in my localhost configuration when trying to run a web project locally. The error message pointed to a chrome process that was locking the port. I assume that rebooting the machine would have fixed the problem, but for me the issue was fixed by closing all instances of Chrome and trying again. Apparently one of them (although I could not find a visible window) was lurking in the background and blocking the port. – TMan Nov 05 '21 at 12:46
1

I recently ran into this exact problem and tried most of the suggestions but it turns out my problem was something different.

Our application has a section that redirects you to HTTPS if you are on HTTP (yes, I know there are better ways but this is legacy). In addition, there is special handling of localhost when you are in dev mode which redirected you back to HTTP, though I don't know why.

I have a new requirement that requires testing in HTTPS.

On a normal web page, changing http:// to https:// and calling Redirect would work fine but in IISExpress you are using custom ports. So for example, if you are on https://localhost:44300 you can't just switch to http://localhost:44300 b/c that port only serves https, not http.

So, if you are having this problem and you've cleared the config files and it didn't help, check your code for redirects. you might have something using a hardcoded or partially hardcoded url or a url from a config file that you haven't updated to https.

Update: I also suggest clearing all .suo and .user files and someone else suggested clearing the obj/ folder. I suggest doing this with VS closed and then restarting. I had a lot of trouble with values for ports being cached when i tried to setup https:// and change ports. I also suggest trying this suggestion to check that the SSL port is within the correct range, though I don't know if that is still a requirement https://stackoverflow.com/a/24957146

Zack
  • 2,291
  • 2
  • 23
  • 38
1

In my case, Visual Studio 2017 > Tools > Options

In Debugging menu in the side list find Edit and Continue

Uncheck the Enable Edit and Continue check box

This resolves my problem.

Nilay Mehta
  • 1,732
  • 2
  • 20
  • 26
1

Same problem here but I think mine was due to installing the latest version of Visual Studio and having both 2015 and 2019 versions running the solution. I deleted the whole .vs folder and restarted Visual Studio and it worked.

I think the issue is that there are multiple configurations for each version of Visual Studio in the .vs folder and it seems to screw it up.

tqrecords
  • 542
  • 1
  • 5
  • 20
1

Sometimes https and http ports can be same because if two tags are given same ports IISExpressSSLPort and DevelopmentServerPort Make sure these two ports have different ports then in IISUrl use your default port either HTTP or HTTPS

https://localhost:44365
  • For HTTPS Use IISExpressSSLPort
  • For HTTP Use DevelopmentServerPort

Then you may delete hidden .vs folder in solution folder

Ali Jamal
  • 844
  • 11
  • 19
1

Try deleting cookies for your localhost

enter image description here

Shaik Matheen
  • 1,233
  • 15
  • 14
1

My problem turned out to be a property which called itself:

enter image description here

Once I fixed this, the misleading connection error message disappeared.

Andy Brown
  • 5,309
  • 4
  • 34
  • 39
1

None of these solutions worked for me. I'm not 100%, but I think my issue might have been with running the same project on the same port from multiple branches. That used to be an issue for me on Local IIS --- I'm not sure if that's also an issue on IIS Express. What worked for me was following this solution:

https://stackoverflow.com/a/27033934/2133723

Which says to go to:

File -> Open -> Web Site...

and remove the relevant projects. After this, I was able to run the project without changing the port.

Prior to doing this, I had followed the other solutions posted on this question (which didn't help me), such as deleting the .vs folder, so I'm not sure if that would be a necessary step prior to removing the website from File -> Open -> Web Site....

Emil
  • 1,131
  • 13
  • 23
0

Like Donny V. this happened to me in VS2017 using .net core 2.0.

When the project first launched, VS displayed a dialog, offering to create an SSL Cert for localhost - but I didn't want to use SSL, so I stopped running and went into Project Properties and disabled SSL (a checkbox that can be cleared). After doing that I kept getting the issue described.

After trying many things what actually resolved the issue for me was editing the launchSettings.json file located under [ProjectName]/Properties - it's accessible from Solution Explorer.

When the editing was complete, the contents looked like this (where 12345 is a Port Number, and where MyNETCoreApp is the name of the project).

{
    "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
            "applicationUrl": "http://localhost:12345/"
        }
    },
    "profiles": {
        "IIS Express": {
            "commandName": "IISExpress",
            "launchBrowser": true,
            "launchUrl": "http://localhost:12345/",
            "environmentVariables": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            }
        },
        "MyNETCoreApp": {
            "commandName": "Project",
            "launchBrowser": true,
            "environmentVariables": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "applicationUrl": "http://localhost:12345/"
        }
    }
}

The edits I made were:

  • Updated the Port Numbers so they were all the same number
  • Removed a key:value of ssl:0 in one spot
  • Updated all https:// instances to be http://
StackOverflowUser
  • 945
  • 12
  • 10
0

I had to add https bindings in my local IIS

Th4t Guy
  • 1,442
  • 3
  • 16
  • 28
0

I did the below in sequence and it worked for me:

  1. delete .vs folder
  2. delete obj folder
  3. close VS Studio
  4. Restart the machine
Arjit Sharma
  • 416
  • 5
  • 15
0

For me the issue was the configuration was set to a custom AsLive instead of Debug.

enter image description here

Setting that back sorted it for me.

JsonStatham
  • 9,770
  • 27
  • 100
  • 181
0

I have had the same problem while on a computer without any admin rights.

Getting rid of the .vs folder (applicationhost.config file) in the project folder was not enough.

What worked for me...I

  • deleted bin folder (as I reused an existing project)
  • deleted Obj folder
  • deleted .vs folder
  • changed the port of the project (I used 3000) as @vinayakhegde mentionned it above and I also created a virtual directory
  • -

Localhost is accessing the solution so far.

Hope it helps

Francois
  • 139
  • 3
  • 15
0

I ran into a similar problem when I started working this morning, using Visual Studio Community 2019, Debug configuration. The IIS Express (Google Chrome) site was unreachable. When I terminated program execution via the red stop button in Visual Studio, I was rewarded with a windows error message stating "process with an id of 4### is not running".

The computer had been left on overnight.

I looked through the answers here and the solutions looked complicated. I tried closing and reopening the solution, restarting Visual Studio and, finally, restarting the computer.

Restarting the computer resolved my issue. I did not delete any files or change any other settings.

IdusOrtus
  • 1,005
  • 1
  • 16
  • 24
0

I had same issue....I tried everything above mentioned but nothing helped.....then I just turned off my windows firewall and it worked for me. So just turn off your Firewall and run Visual Studio as Administrator.

Subhrut Taori
  • 27
  • 1
  • 2
  • 11
0

My reason was that when I updated applicationHost.config manually, I deleted also

<add name="DefaultAppPool" managedRuntimeVersion="v4.0" />

but the line

<applicationDefaults applicationPool="DefaultAppPool" />

remained there. So obviously when I added some app, IIS got corrupted and refused to connect to any apps, even those which were set to another app pool.

So, please, make sure you have a valid pool for all applications.

Frimlik
  • 357
  • 3
  • 15
0

I had a similar issue on VS 2017 and spent days trying to figure out what the cause of this issue was. I tried deleting the hidden .vs folder and that didn't work for me. I turned off script debugging. I spent days trying lots of different things to fix this issue to no avail. I would change ports and it would occasionally work after hours and that was with me trying the same port numbers. Sometimes a popup from visual studio would show and say that this port was in use .This popup would show on a very rare occasion but when it did I was able to run my project again. Once my laptop shut down the issue would crop up again and the same steps to fix would not be applicable. I could not deduct what the cause was any of these times. It seemed to fix itself randomly but only after that pop up showed up. It seemed to show randomly if anything.

I ended up downloading VS 2019 and my project worked out of the box there. If that is not possible for you maybe reinstall VS 2017.

0

In Visual Studio 2022 version 17.2.5

  1. Right click the project
  2. Properties
  3. Debug
  4. Open debug launch profiles UI
  5. App URL: change to https://localhost:44366

Nothing else worked for me, because I couldn't find the option to change this URL, because it seems like this version of VS changed the user interface.

mikl
  • 1,067
  • 1
  • 20
  • 34
0

I had this same issue when using Chrome as my client browser, but my remedy is slightly different from other solutions that use netsh. The following worked for me (with a reboot after).

netsh int ip reset

I did not have the same issue in Firefox (and presumably other client browsers), and as far as I could tell, it was only Chrome having issues. The symptoms suggested a problem with the self-signed certificate I was using, but this was ruled out by checking other browsers; and no other processes were interfering with the local listening port.

This MS Support article related to my remedy suggests there are other ways to encounter this issue in this form.

meklarian
  • 6,595
  • 1
  • 23
  • 49
0

Yet another thing to try, if on Windows: check your hosts file in C:\Windows\System32\drivers\etc. Ensure that you have localhost.companyName.com specified (where companyName is your company), rather than just localhost. For me, that's all it took, (everything else already being in place).

johnjps111
  • 1,160
  • 9
  • 24
-1

This solution worked for me:

  1. Go to your project folder and open .vs folder (keep your check hidden item-box checked as this folder may be hidden sometimes)

  2. in .vs folder - open config

  3. see that applicationhost config file there? Delete that thing.(Do not worry it will regenerate automatically once you recompile the project.)

Quoc Nguyen
  • 2,839
  • 6
  • 23
  • 28
Ven Y
  • 1
  • 1
  • please do not copy and paste the answers that are already there. If an answer helps you just upvote that answer. – Amir Dora. Apr 22 '21 at 18:52