views:

506

answers:

4

I want to know if there is a recommended way of determining if an asp application is running locally. At the moment I use the Request object and do a string search for localhost or 127.0.0.1 on the server variable but this has several limitations. The biggest one being that the Request object is not always available when I need it.

+3  A: 

You can check the Request.IsLocal property

Adam
+10  A: 

See HttpRequest.IsLocal

bool isLocal = HttpContext.Current.Request.IsLocal;
Rex M
+1  A: 

Request is not always available in ASP.NET environment?

HttpContext and its properties Request/Response are initialized as soon as the server starts processing the page. So at any place you can execute c# code in your page life cycle you should be able to check the request url.

Roman R.
I didn't realise that I could use the HttpContext class to access the Request object.
Sean
Out of curiosity, what other method of accessing the Request object is available? Thanks :)
Roman R.
System.Web.UI.Page.Request
Sean
A: 

Request.IsLocal is the same as checking for 127.0.0.1 or ::1. See this post: http://forums.asp.net/p/1065813/4081335.aspx.

ZLA
Yes, but using a standard library call conveys the intention of the code better, IMO. I would prefer to use the library rather than write my own code to do such a simple thing.
Sean
I agree. I just wanted to point out that since the designated answer may be the same code as what the poster was using, the answer may have the same limitations.
ZLA