How to determine if .NET code is running in an ASP.NET process?

后端 未结 6 1689
一生所求
一生所求 2020-12-17 15:19

I have an instance of a general purpose class that will be executed both under ASP.NET and a stand alone program. This code is sensative to the process where it is being run

6条回答
  •  感情败类
    2020-12-17 15:30

    This is my answer to the question.

    First, make sure your project references System.Web and that your code file is "using System.Web;".

    public class SomeClass {
    
      public bool  RunningUnderAspNet    { get; private set; }
    
    
      public SomeClass()
        //
        // constructor
        //
      {
        try {
          RunningUnderAspNet = null != HttpContext.Current;
        }
        catch {
          RunningUnderAspNet = false;
        }
      }
    }
    

提交回复
热议问题