Monday, August 17, 2009

How do you programmatically determine if an ASP.NET application is running locally

A user recently popped up a question on the forums asking if it was possible to determine that his asp.net app is running on the local machine. Well there were suggestions to check for the local address 127.0.0.1 or check http://localhost.

How to do this the reliable way, a very simple solution is to use the 'isLocal' property of the Request class as shown below:

C#


protected void Page_Load(object sender, EventArgs e)

{

if (HttpContext.Current.Request.IsLocal)

{

Response.Write("App is local");

}

}



VB.NET


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

If HttpContext.Current.Request.IsLocal Then

Response.Write("App is local")

End If

End Sub

No comments:

Post a Comment