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
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
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.
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.
IHostingEnvironment
determines the environment at run-time. They are both valid approaches, but for different problems.