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







About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

Kiosker said...

Verry good tip