Is there a directive for ASP.NET discovery?

I have a code library that works in ASP.NET, SQL CLR, and standalone applications and provides various functions based on whether certain namespaces are available (e.g. System.Drawing ) or not. Right now, I am excluding these code snippets manually, but it would be useful to have a C # compiler with it:

  • I could be lazy
  • I could use the same library.

I know that I can use the #if directives to search for definitions, and I could manually define something like ASP_NET , but if the way to do this is automatic, it will be even more.

So, can I detect ASP.NET? Alternatively, is it possible to determine if certain referenced assemblies are available?

+4
source share
2 answers

There is no such predefined preprocessor directive for asp.net.

What most people do is search for the current HttpContext - the assumption is that if it is null, it is not a web context.

Another alternative is testing HttpRuntime.AppDomainAppId for null, to a similar assumption.

Other similar options:

 System.Web.Hosting.HostingEnvironment.IsHosted == true System.Web.HttpRuntime.Cache != null 

And you can check that the web.config file exists.

+9
source

If you need to do this at compile time (as you said), you can define the preprocessor symbol yourself.

Pre-processing is a C # concept. You need to compile once for each goal you want to run. The preprocessor cannot be used for anything that happens at runtime. This result is burned in binary format.

0
source

All Articles