Get Server Name and Server Port No in Asp.Net

As we know that there is a need to get server name or port no at run time that request came from where, is port no exist or not to make application dynamic URL, assume when we are working in
development environment and deploying project at production environment then we have to change URL path or manage that URL in web config and further at production environment need to change it so here is the better way make it dynamic receive from request header that which url hit the server..


 string ServerName=Request.ServerVariables["server_name"];
 string PortNo = Request.ServerVariables["SERVER_PORT"];
 string url = "http://" + ServerName;

 if (string.IsNullOrEmpty(PortNo))
 {
     url += ":" + PortNo + "/WebSiteDomainName";
 }
  url += "/login.aspx";


Popular Posts