Sean
Sean

Reputation: 1522

Determine if ASP.NET application is running locally

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.

Upvotes: 86

Views: 42024

Answers (9)

Thomas
Thomas

Reputation: 726

I approached this by creating an entry in .gitignore for a secret json file, appsettings.secrets.json:

{
  "Environment": "localhost"
}

Then in Program.cs, utilize this to check if it is local or not:


builder
    .Configuration.
    .AddJsonFile(
        "appsettings.secret.json", 
        optional: true,
        reloadOnChange: true
    );

...

bool isLocalDev = builder.Configuration["Environment"] == "localhost";

if (isLocalDev)
{
    // Local configuration
}

Doing it this way will disallow any remote runner from enabling a local version and no deployment changes are required.

Upvotes: 1

Ulysses Alves
Ulysses Alves

Reputation: 2469

In response to @Meh Men's comment for other answer in this thread, who asked:

What about where Request is null. i.e: Application_start?

If you are sure your production and test or "homolog" versions of your website will all be deployed with a release version of your website, while your local environment will be built and developed in "debug" mode, you can make use of #if DEBUG sintax to write code which only should be run locally, while outside of this block, or even inside a matching #else block, you may write some other code which you want to be run only when not locally (e.g: remotely).

Here is a small sample of how I've solved this problem in a particular project I'm curreltly working on:

#if DEBUG
    // Code here will only be run locally.
#else
    // Code here will only be run "remotely".

Upvotes: 5

Damian Vogel
Damian Vogel

Reputation: 1182

In a MVC view / ASP page / code behind class:

bool isLocal = HttpContext.Current.Request.IsLocal;

In an MVC controller :

bool isLocal = Request.IsLocal;

Upvotes: 4

Sumanth
Sumanth

Reputation: 214

This worked for me with Application_Start

if (!HostingEnvironment.IsDevelopmentEnvironment)
{
      GlobalFilters.Filters.Add(new RequireHttpsAttribute());
}

To know more about how IsDevelopmentEnvironment is set, please look at the following thread.

In ASP.NET, what determines the value of HostingEnvironment.IsDevelopmentEnvironment?

Upvotes: 8

Matteo Migliore
Matteo Migliore

Reputation: 923

If HttpContext.Current is not null use

HttpContext.Current.Request.IsLocal

Otherwise, for example in the App_Start or before HttpContext.Current is available, you can test

HostingEnvironment.ApplicationPhysicalPath.StartsWith(@"C:\")

or a dedicated disk on your PC.

Another way can be use a constant compilation variable set in production, for example from Azure and visualstudio.com if you use them.

It is dirty, but it works.

Upvotes: 1

ZLA
ZLA

Reputation: 19

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.

Upvotes: 1

Roman Royter
Roman Royter

Reputation: 1665

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.

Upvotes: 0

Rex M
Rex M

Reputation: 144122

See HttpRequest.IsLocal

bool isLocal = HttpContext.Current.Request.IsLocal;

Upvotes: 155

Adam
Adam

Reputation: 2004

You can check the Request.IsLocal property

Upvotes: 14

Related Questions