20

Back from my weekend and went to debug my web project which is an ASP.NET Core Web Api. It started giving me an error: Invalid URI: The hostname could not be parsed.

I can start a new asp.net core web api project and it debugs fine so I'm pretty sure its something with my configuration for this project. Any ideas?

James

James Scott
  • 990
  • 2
  • 10
  • 29
  • my issue was trying to run the dll by path. you have to set your working directory to the publish dir (with the dll and all the supporting files) then dotnet .dll and it works – vampiire Mar 30 '20 at 17:49

7 Answers7

29

Solution:

  1. Close Visual Studio
  2. Remove .vs folder

Fixed:)

szubajak
  • 455
  • 7
  • 12
  • If you have a new question, please ask it by clicking the [Ask Question](http://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/low-quality-posts/16129595) – Jignesh Ansodariya May 16 '17 at 09:35
  • 4
    @JigneshAnsodariya The OP had the same problem and explains how they solved it. It answers the question. – BSMP May 16 '17 at 15:21
  • 1
    Worked - but might have to re-choose your solution's startup project afterwards – Lavamantis Apr 14 '21 at 23:05
  • you have saved me one more time today :) – Muhammad Saqib Mar 23 '22 at 16:44
16

I post this answer because many people found my comment useful. Thanks to @joshcomley for poking me.

Hopefully this answer will help someone:

  1. In your solution folder find subfolder named .vs (note, this folder has a hidden attribute, so File Explorer does not show it by default)
  2. Open .vs/config/applicationhost.config file
  3. Inside the file find element like <site name="<Your_Web_Site_Name>" ..... You can have several elements like this. You need to pick one where <Your_Web_Site_Name> matches the name of your site
  4. Inside <site element find a child element like:

<binding protocol="http" bindingInformation=":8080:localhost" />

and replace it with:

<binding protocol="http" bindingInformation=":8080:" />

  1. Rebuild solution and Run website

Notes:

  • The port number 8080 is given as an example. You should assign the port that you actually use for the website.
  • This fix works for websites hosted in IISExpress. It also allows to avoid error message like Invalid URI: The hostname could not be parsed.
  • If your website is using IIS, you may try to replace binding with this line: <binding protocol="http" bindingInformation="*:8080:*" />. And do iisreset after this change.
VeganHunter
  • 5,584
  • 2
  • 26
  • 26
  • 5
    I believe `bindingInformation="*:8080:*"` doesn't parse, but `":8080:"` does, as does `":8080:localhost"` – QuickDanger Jun 04 '18 at 16:13
  • @QuickDanger, please try to do iisreset after the change. Works for me. – VeganHunter Jun 05 '18 at 02:10
  • 3
    For anyone looking, this is the answer to ["How do I allow IIS Express to accept remote connections?"](https://stackoverflow.com/questions/3313616/how-to-enable-external-request-in-iis-express) at least on Windows 10 & VS 2017. I don't know if this changed, but there's now a lot of misinformation out there recommending the "star" syntax, which seemingly can no longer be parsed by IIS Express. Thanks @VeganHunter! – Mark Jan 16 '19 at 19:38
6

I was having the same issue and tried other suggestions in this post, but it didn´t work. So I went to Properties of the project -> Debug -> WebServer Settings There I´ve replaced the * with localhost and that solved the issue for me. Properties capture

Javier Pazos
  • 119
  • 2
  • 6
  • 2
    This is a proper way to do it, because it handles .net core projects as well (these don't have a file named `.vs/config/applicationhost.config`, but `launchSettings.json` instead - at least our project didn't). – Mladen B. Sep 28 '20 at 21:35
5

Jakub's answer does fix the issue because it causes Visual Studio to regenerate applicationhost.config. In my case, the error was saying that the value of the <binding> tag could not be parsed. I had the following: <binding protocol="http" bindingInformation="*:5000:*" /> which when regenerated looked like this: <binding protocol="http" bindingInformation="*:5000:localhost" /> The file is found inside the ".vs" folder's "config" subfolder, and instead of deleting the whole directory, you can just fix/restore your <binding> tag to a value that can be parsed.

Edit: per @VeganHunter's comment, * is not a valid hostname. If you want it to bind to all hostnames just leave the host blank. Ex: *:80: instead of *:80:* means "All network interfaces/IPs, on port 80, for all hostnames".

QuickDanger
  • 1,032
  • 11
  • 18
  • 15
    There is important note here. When you have the line IISExpress can only be accessed as a localhost, it cannot be accessed from the network by IP address. To make it work via IP address some people advise to put the line like you mentioned: Which actually did not work for me (and you) because VS starts showing "Invalid URI: The hostname could not be parsed" error after restart. What did work for me is this: – VeganHunter Jul 19 '17 at 00:45
  • 1
    @VeganHunter please post that as an answer! It's very useful for a lot of people and I believe would have solved OPs question. – joshcomley Feb 20 '18 at 14:15
2

My answer is a variant from @Javier Pazos

In my case, going to the the project properties, my URI was correct, enter image description here

However, my Environment Variables were wrong (this happened after updating VS) enter image description here

So, here replace the "*" for localhost

DReact
  • 606
  • 6
  • 7
1

This error indicates that your bindingInformation for the site is not in the correct format.

The value for bindingInformation consists of three parts.

ip:port:host

Here are some formats of bindings and their result:

<!-- Configures the site with a hostname of "www.test.com" on port 80 for the IP address of 192.168.1.10. -->
<binding protocol="http" bindingInformation="192.168.1.10:80:www.test.com" />

<!-- Configures the site with a hostname of "localhost" on port 80 for the IP address of 192.168.1.10. -->
<binding protocol="http" bindingInformation="192.168.1.10:80:localhost" />

<!-- Configures the site without a hostname and IP address on port 80. You'd use this setting to make your site directly accessible via any address that the system has, including the localhost address at 127.0.0.1, as well as any and all configured IP addresses. -->
<binding protocol="http" bindingInformation=":80:" />

<binding protocol="https" bindingInformation="*:443:" />
<!-- Configures HTTPS bindings for all IP addresses over port 443. -->

With all the above bindings, you can set the protocol to https to make your site accessible only through SSL.

chaosifier
  • 2,666
  • 25
  • 39
0

I was getting similar issue and running the Visual Studio in Administrator mode resolved the issue for me.

Ayush
  • 485
  • 2
  • 6
  • 19