In IIS-Manager, the default web site is bound on port 80. How can I get the port by website-name, using c#? I tried the following code:
var lWebsite = "Default Web Site";
var lServerManager = new ServerManager();
var lPort = lServerManager.Sites[lWebsite].Bindings.GetAttributeValue("Port");
lPort
results in null with an invalid index
exception. But the assignment var lPort = lServerManager.Sites[lWebsite]
works.
open the chrome browser, open developer tools in chrome , Put the url in search bar and hit enter. look in network tab, you will see the ip and port both.
Web server ports are the logical endpoints of a network connection that is used to exchange information between a web server and a web client. These ports are assigned a unique number to identify them separately. Port 80 and 443 are the two standard ports used by web servers to communicate with the web clients.
Go to Start and open Administrative Tools. Double-click Internet Information Services (IIS) Manager. In the Internet Information Services dialog box, expand local computer ►Sites and right click Default Website and select Bindings. Select the http setting and click Edit.
In Solution Explorer, right-click the name of the web application and select Properties. Click the Web tab. In the Servers section, under dropdown selection for IIS Express, change the port number in the Project URL box. To the right of the Project URL box, click Create Virtual Directory, and then click OK.
If you know the url, 1. open the chrome browser, 2. open developer tools in chrome, 3. Put the url in search bar and hit enter 4. look in network tab, you will see the ip and port both
It can be used for getting the port, domain, and protocol as it has inbuilt methods to get these values. The url.protocol property is used to return the protocol scheme of the URL along with the final colon (:). The url.hostname is used to return the domain of the URL. The url.port property is used to return the port of the URL.
The USB driver should have a property page in the Device Manager that shows (or let you change) the assigned port number. Another way to enumerate available COM ports is through a WMI query, namespace root\CIMV2, class Win32_SerialPort. Lots of extra stuff there, like the Caption property, to help your user select the correct port.
If you want to get access to the port number used by each binding, you need to loop through those bindings and ask each one for its associated port number:
When you access Sites[lWebsite].Bindings, you're accessing a collection of bindings. When you then try to call GetAttributeValue("Port"), it fails because that makes no sense - the collection itself doesn't have a port number associated with it, it's just a collection.
If you want to get access to the port number used by each binding, you need to loop through those bindings and ask each one for its associated port number:
var site = lServerManager.Sites[lWebsite];
foreach (var binding in site.Bindings)
{
int port = binding.EndPoint.Port;
// Now you have this binding's port, and can do whatever you want with it.
}
It's worth highlighting the fact that websites can be bound to multiple ports. You talk about getting "the" port, but that's not necessarily the case - for example, a website that's served over both HTTP and HTTPS will have two bindings, usually on ports 80 and 443. That's why you're having to deal with a collection of bindings, even though in your case that collection may only contain one binding it's still a collection.
For further details, have a look at the MSDN documentation for the Binding class. Note that some of what you're likely to be interested in will involve accessing the binding's EndPoint property, as in the sample above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With