REST - Network // Web Application framework

Markus Freitag 3,521 Reputation points
2023-10-26T18:44:50.3533333+00:00

Hello,

Works well https://localhost:44383/api/values

<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>value1</string>
<string>value2</string>
<string>TESTTTTT</string>
</ArrayOfstring>

Works not, why? https://127.0.0.1:44383/api/values

Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid.

cmd -> ipconfig -> IPv4-Address . . . . . . . . . . : x.x.x.x

If I want to enter the IP, how do I get it. Inside project, setting I use this. https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/working-with-ssl-in-web-api SSL Enabled ASP.NET, .NET4.7.2 Framework.

My idea is, I run the application on my PC. Then connect to the PC that has no development environment on it and query the data.

Advantage. It saves me the complicated setup. Map network to PC.

https://support.microsoft.com/en-us/windows/map-a-network-drive-in-windows-29ce55d1-34e3-a7e2-4801-131475f9557d

public class ValuesController : ApiController
{
	// GET api/values
	public IEnumerable<string> Get()
	{
		return new string[] { "value1", "value2", "TESTTTTT" };
	}

	// GET api/values/5
	public string Get(int id)
	{
		return "value";
	}

	// POST api/values
	public void Post([FromBody] string value)
	{
	}

	// PUT api/values/5
	public void Put(int id, [FromBody] string value)
	{
	}

	// DELETE api/values/5
	public void Delete(int id)
	{
	}
}
Internet Information Services
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
1,716 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,457 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yurong Dai-MSFT 2,246 Reputation points Microsoft Vendor
    2023-10-31T06:01:44.8666667+00:00

    Hi @Markus Freitag,

    If you use Visual Studio's built-in web server (IIS Express), localhost is mapped by default. If you need to access the application through 127.0.0.1, you need to configure it. The following is the test I did:

    I created a default project in VS, and when I choose to run this project using IIS express, I can access it normally via https://localhost:44365.

    User's image

    But if I change localhost to 127.0.0.1, the same error as yours will appear.User's image

    This is because I didn't configure it in the project. Next do the following.

    1.Edit applicationhost.config located inside .vs\config (".vs" dirctory is hidden) in your project and add bindings for 127.0.0.1 for http and https:

    <sites>
        ...
        <site name="asp.net test" id="2"> <!-- your asp.net project -->
            ...
            <bindings>
              <binding protocol="https" bindingInformation="*:44365:localhost" />
                <binding protocol="http" bindingInformation="*:55250:localhost" />
                <binding protocol="https" bindingInformation="*:44365:127.0.0.1" />
                <binding protocol="http" bindingInformation="*:55250:127.0.0.1" />
            </bindings>
        </site>
          ...
    </sites>
    

    2.Edit Project Url settings in VS: from solution explorer under the project tree > [your project] > properties > Web: change localhost to 127.0.0.1.

    User's image

    3.Save the changes and re-run the application, this time I can access the application using 127.0.0.1.

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the email notification for this thread.

    Best regards,

    Yurong Dai

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 48,561 Reputation points
    2023-10-26T22:11:26.2666667+00:00

    websites commonly use a host header to determine the site. this was done so a single webserver can host multiple domain names. if hostname binding is used, often the machine ipaddess will not work also.

    you web server has probably no mapping for the loopback ipaddress.

    website binding uses ipaddress, hostname, port and protocol (http/https). the ipaddress or hostname can be wildcarded.

    0 comments No comments

  2. Lex Li (Microsoft) 4,031 Reputation points
    2023-10-27T03:44:08.3966667+00:00

    Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid.

    That's expected when you run such a web app on IIS Express, as most tools (you might be using Visual Studio) configure IIS Express to use a site binding that only supports https://localhost:44383.

    If you, however, want that site on IIS Express to be accessed via URLs with IP addresses, you need to explicitly change the bindings, after learning how IIS site bindings work,

    https://docs.jexusmanager.com/tutorials/binding-diagnostics.html#background

    Another essential thing you need to pay attention to is that https://x.x.x.x:44383 is not well served everywhere, since no Certificate Authorities is going to provide you a valid certificate for an IP address.

    0 comments No comments

  3. Yurong Dai-MSFT 2,246 Reputation points Microsoft Vendor
    2023-10-27T07:19:26.9233333+00:00

    Hi @Markus Freitag,
    Is your project deployed in IIS? Did you modify anything in the hostsfile? Please make sure you include 127.0.0.1 localhost in your hosts file. Have you tried pinging 127.0.0.1 to see if there is any sort of response?

    If you can access the website using localhost, but cannot connect to it using 127.0.0.1, try the following steps:

    1. Run command prompt and type “netsh”.
    2. Type “http”.
    3. Use the command below to configure IIS to listen on 127.0.0.1. netsh http>add iplisten ipaddress=127.0.0.1

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the email notification for this thread.

    Best regards,

    Yurong Dai