سؤال

I'm running some code in the Application_Start block within my global.asax and I'm looking for a way in which I can determine if the app is running locally so I can conditionally execute the code.

Normally I'd use something like this, but there is no httpcontext in the global.asax:

   if (Request.IsLocal == true) {
        //run the code...
   }

Is there another way in which I can determine if the app is running locally? Debug would always be set to true on the localhost, so perhaps that will give me some handle I can use?

EDIT 13th Dec

I should have clarified BeginRequest is not a suitable candidate here as the code being executed is writing a number of files to the local directory and this shouldn't be repeated on every request.

هل كانت مفيدة؟

المحلول

This one is specifically to your question about determining debug from web.config:

var configSection = ConfigurationManager.GetSection("system.web/compilation");
if (configSection.Debug) {
    // your code
}

I think you would also need to cast that appropriately. Just off the hand.

Yup. You got to cast it to System.Web.Configuration.CompilationSection.

نصائح أخرى

Ok decided to go with this, @abhitalks's solution also works!

   if System.Diagnostics.Debugger.IsAttached() {
        //....
   }

This worked for me.

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?

Easiest way to determine or categorize both is placing the required code condition to be executed locally in Application_Start, where as if it is to be executed not locally, then place it in the BeginRequest of Global.asax.

I faced this issue couple of days back and could get instant reply online.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top