Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the port by websites name via C# from IIS?

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.

like image 875
Simon Avatar asked Apr 07 '14 12:04

Simon


People also ask

How do I find the port number for a website?

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.

What is the port of a website?

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.

How do I find my IIS port?

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.

How can I change IIS Express port for a site?

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.

How to find the IP address and Port of a website?

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

How do I get the port and domain of a URL?

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.

How do I find the COM port of a device?

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.

How do I get the port number of each binding?

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:


1 Answers

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.

like image 66
Chris Avatar answered Oct 26 '22 00:10

Chris