7

With the debug setting gone in the web.config, what setting turns on and off debug and what is the equivalent (if any) for the following in .Net 5 (MVC 6 project)?

#define DEBUG
// ...
#if DEBUG
    Console.WriteLine("Debug version");
#endif
5
  • The web.config does not contain #DEBUG. #DEBUG is a compiler directive, not a web.config setting. Can you rephrase your question?
    – Igor
    Commented Feb 11, 2015 at 21:13
  • @Igor - There is no web config in vNext projects
    – user2095880
    Commented Feb 11, 2015 at 21:15
  • I stand corrected, it is possible to define in the 4.5 version of .NET in the web.config in section compiler attribute compilerOptions.
    – Igor
    Commented Feb 11, 2015 at 21:20
  • Thanks Igor but I'm talking about .Net 5 which uses Project.json files.
    – RickJames
    Commented Feb 11, 2015 at 21:23
  • @RickJames - I understood, I was commenting that my previous comment was not right :). Can you post a link to the source about this? The only thing I was able to find is that they are removing it from the web.config but maybe you can still put it in your code directly like you did up in your example and pass the directive in to your compiler at compile time?
    – Igor
    Commented Feb 11, 2015 at 21:24

2 Answers 2

7

In your project json file, you need to add:

"frameworks": {
    "aspnet50": {
        "compilationOptions": {
            "define": [ "WHATEVER_YOU_WANT_TO_CALL_IT" ]
        }
    },
    "aspnetcore50": {
        "compilationOptions": {
            "define": [ "WHATEVER_YOU_WANT_TO_CALL_IT" ]
        }
    }

then in your code you use it as follows:

#if WHATEVER_YOU_WANT_TO_CALL_IT
    .. your code..
#endif

where WHATEVER_YOU_WANT_TO_CALL_IT can = DEBUG or whatever else.

3
  • Does it possible to have for single framework two different configuration?
    – dotnetstep
    Commented Feb 12, 2015 at 16:33
  • If I understand correctly, I don't see why not, but I have not tested it yet.
    – user2095880
    Commented Feb 12, 2015 at 18:33
  • Seems that this is deprecated and now we should use "buildOptions" according to a compiler warning reported
    – pqsk
    Commented Nov 28, 2016 at 19:38
5

UPDATE

Since writing this answer I have learned that the new way in .Net Core is to use Environment Variables. You can find an article here and more info here.

You can set the environment variable in your project properties under debug. The code would look like after using DI to inject IHostingEnvironment

if (env.IsDevelopment())
{
    //...
}

END UPDATE

The answer by @user2095880 is valid and does work. However, you may want a solution that you do not need to change the project.json to go to production.

#if DEBUG
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello DEBUG CODE!");
            });
#else
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello LIVE CODE!");
            });
#endif

This checks your solution configuration (still works in .Net 5) if you are in Debug or something else. If your solution configuration is set to Debug, the first set of code will run. If you select Release (or anything else), the second code section will run. See the image below for the dropdown to change from Debug to Release.

enter image description here

1
  • The difference here is that the pre-processor directives are determining the target environment at compile-time. The solution involving the IHostingEnvironment determines the environment at run-time. They are both valid approaches, but for different problems.
    – Matt
    Commented Sep 4, 2016 at 18:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.