When we work on a File-system based web site, the application is run on the localhost with a port number. E.g. http://localhost:12345/TaskManagerService.aspx.
This port number is a randomly chosen one when we run the web application.
Even when we restart the web application (restart the visual studio), this port number stays as is for every run. It usually doesn’t change. This is because of the following behavior of the Visual Studio.
The port number used to host the web application is stored in the project file (.csproj or .vbproj) under the tag, <DevelopmentServerPort>.
When a web application is initially created, this Development Server Port is initialized to zero. <DevelopmentServerPort>0</DevelopmentServerPort>
But when the application is run for the first time, an available port is randomly chosen and assigned to this web application. That’s how we get the default website address starting with http://localhost:12345.
The chosen port number is stored in the project file for any future runs. <DevelopmentServerPort>12345</DevelopmentServerPort>. During every run of the web application, Visual studio looks for a port number in the project file and sees if the port is free for use. If the port is available, the same port number is used for hosting the application.
Changing the default Development Server Port Number
For changing the port number follow these steps.
1. Open the .csproj or .vbproj file with a regular text editor (e.g. Notepad).
2. Browse for the tag <DevelopmentServerPort> and change the value in it.
1. In Visual Studio click on the Project Menu and Select <ProjectName> Properties…
2. Select the Web tab.
3. Under the Servers group, we would find the Use Visual Studio Development Server option.
4. Under this option we can select the Specific Port option and specify the port number of our choice.
Note: Though we have the option to chose the port number, there is no guarantee that the number of our choice would be available. So on running the application, it may likely be changed by the visual studio.