Search code examples
asp.net-corevisual-studio-2017asp.net-core-mvcvisual-studio-2019

How to debug ASP.NET Core webapp using a URL other than localhost


I have a web application that behaves differently depending on the hostname. To test it on my local PC, I will then need to arrange for IISExpress to run the web application using a specific name that I provide in Project -> Properties -> Debug, such as http://mywebsite:58981/.

enter image description here

But when start debugging, a pop up message "Unable to connect to web server 'IIS Express'" pops up. And Windows 10 activity center alerted me of a message and upon clicking it, I received this pop-up from IIS Express:

enter image description here

I tried what other users suggested such as deleting the .vs folder containing the applicationhost.config and restarted the solution, but it still doesn't work. I even made sure that IIS Express doesn't have anything else running:

enter image description here

But if I changed the App URL back to http://localhost:58981/, deleted the .vs folder, and open the solution, I can debug as usual.

So my question is: how do I change the URL from localhost to something else? I'm running Visual Studio 2019 and my Windows 10 is 1903 edition.


Solution

  • The first advise is run Visual Studio as Administrator.

    Modify the IIS Express configuration files , go to edit file :

    \{project folder}\.vs\{yourWebApplicationName}\config\applicationhost.config

    You will find something like this(find with your application name):

    <site name="WebApplication3" id="2">
        <application path="/" applicationPool="WebApplication3 AppPool">
          <virtualDirectory path="/" physicalPath="C:\Users\Administrator\source\repos\WebApplication3\WebApplication3" />
        </application>
        <bindings>
          <binding protocol="http" bindingInformation="*:53717:localhost" />
          <binding protocol="https" bindingInformation="*:44325:localhost" />
    
        </bindings>
    </site>
    

    Add in <bindings>, add another row with your own IP, port number:

    <binding protocol="http" bindingInformation="*:53717:mywebsite" />
    

    Go to C:\Windows\System32\drivers\etc edit the hosts file (copy to another place ,edit and copy back):

    # localhost name resolution is handled within DNS itself.
        127.0.0.1       mywebsite
    #   ::1             localhost
    

    Then you could use http://mywebsite:53707 to debug your application , you may comment out app.UseHttpsRedirection(); in Configure to handle HTTP request .