localhost refused to connect Error in visual studio

240,625

Solution 1

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.)

Solution 2

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

Solution 3

Changing port number has resolved the issue

enter image description here

Solution 4

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.

Solution 5

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

Share:
240,625

Related videos on Youtube

duke
Author by

duke

When it comes to gain knowledge : stay hungry, stay foolish

Updated on July 08, 2022

Comments

  • duke
    duke almost 2 years

    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?

  • WillC
    WillC over 7 years
    This worked for me. You may need to start VS in Administrator mode.
  • Baimyrza Shamyr
    Baimyrza Shamyr over 7 years
    I am getting error "Unable to launch IIS Web server".
  • Nathan Tregillus
    Nathan Tregillus over 6 years
    tried this, still getting the "This site can't be reached" message. (I also tried to open in admin mode, didn't help)
  • Gags
    Gags almost 6 years
    Worked for me as well
  • Diomos
    Diomos over 5 years
    this actually helped. I had combined issue: Fiddler running and certificate missing.
  • jstuardo
    jstuardo over 5 years
    That is solved momentarily. I get bored changing the port every time I run the project.
  • Osify
    Osify over 5 years
    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.
  • netchicken
    netchicken over 5 years
    Using VS 2019 Preview 2. Got the same error, and fixed it by changing to the Project URL above. (goodness knows why) :-)
  • MusicAndCode
    MusicAndCode about 5 years
    Didn't worked until restart of VS 2017. No need for Admin mode.
  • Christine
    Christine about 5 years
    Process with an Id of 27456 is not running.
  • moomoo
    moomoo about 5 years
    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.
  • Dr. Hilarius
    Dr. Hilarius about 5 years
    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.
  • tagustini
    tagustini almost 5 years
    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.
  • tdbeckett
    tdbeckett over 4 years
    the file was in more than one place. In addition to this, I had to restart VS.
  • Vitaliy Markitanov
    Vitaliy Markitanov over 4 years
    in my case i had to run on different port, on 44343. IisExpressAdminCmd.exe setupsslUrl -url:localhost:44343 -UseSelfSigned
  • William
    William over 4 years
    I actually had to delete the whole .vs folder and change the port number, but at least eventually it worked
  • Quiver
    Quiver over 4 years
    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!
  • Mahender Reddy Yasa
    Mahender Reddy Yasa almost 4 years
    @Kush I'm trying to debug react app with chrome extension and I don't see .vs folder even in hidden items but see .vscode folder which has launch.json, is this solution specific to only .net or react as well
  • Dmitresky
    Dmitresky over 3 years
    Deleting .vs folder didn't help me. But repair helped. Thanks a lot
  • Ben
    Ben over 3 years
    Also possible two instances of Visual Studio with different projects are running on the same port. Making the ports different worked for me.
  • oneberenjena
    oneberenjena over 3 years
    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!
  • Nard Dog
    Nard Dog over 3 years
    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.
  • useruser00
    useruser00 over 3 years
    My application was working fine until recently, started getting "this site cant be reached" messages in browser.... this solved my issue
  • Will Strohl
    Will Strohl about 3 years
    I can't believe this was the fix for me. I've never run into this before. 🤷‍♂️
  • Amir Dora.
    Amir Dora. about 3 years
    please do not copy and paste the answers that are already there. If an answer helps you just upvote that answer.
  • E-A
    E-A almost 3 years
    None of the above and other sites solved my problem but this one did!
  • Ravi Ram
    Ravi Ram over 2 years
    After 2 days, reading and trying all sorts of things, IISExpress Repair fixed it. I am able to run .Net Core 5 WebApp out of the box setup. Finally
  • loonix
    loonix over 2 years
    Mine worked with your and @SharpC answer combined
  • Dylan Karimagoko
    Dylan Karimagoko over 2 years
    Thanks champ this actually worked thanks a lot
  • TMan
    TMan over 2 years
    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.
  • corix010
    corix010 over 2 years
    @jstuardo interesting, mine was solved permanently. I don't have to change it every time.
  • Mohammadreza Askari
    Mohammadreza Askari over 2 years
    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.
  • Anthony rono
    Anthony rono almost 2 years
    this one did it for me
  • Cheung
    Cheung almost 2 years
    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.