I can't swap to Development Environment

Arin Leviti 60 Reputation points
2024-01-17T16:46:04.03+00:00

I published a website on a virtual machine, as it was the only way I could run a small exe console app I wrote on c#. I'ts a small game with inputs and outputs that I adapted to work on the page. When I try the page on my local machine it works fine, but when I click the azure link, I get this message: Error. An error occurred while processing your request. Request ID: 00-b880647b6615cb554652a10e6549f17b-80d41d9feb78848e-00 Development Mode Swapping to the Development environment displays detailed information about the error that occurred. The Development environment shouldn't be enabled for deployed applications. It can result in displaying sensitive information from exceptions to end users. For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development and restarting the app. I tried everything to switch environment on my VM but nothing seems to work. I can't swap to Development. Yesterday this problem simply was not there, and today it appeared out of nowhere...

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,308 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,521 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,205 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pinaki Ghatak 2,555 Reputation points Microsoft Employee
    2024-01-18T14:52:01.8866667+00:00

    Hello @Arin Leviti The error message you’re seeing is a common one in ASP.NET Core applications when an unhandled exception occurs. It suggests switching to the Development environment to get more detailed information about the error. To switch the environment on your Azure virtual machine, you can try the following steps:

    1. Set the ASPNETCORE_ENVIRONMENT variable: You can do this in the Azure portal. Go to your Web App’s configuration, find the “Application Settings” section, and add the “ASPNETCORE_ENVIRONMENT” variable with the value "Development".
    2. Modify your Startup.cs file: In the Configure() method of your Startup.cs file, you could try adding app.UseDeveloperExceptionPage();. This will enable the Developer Exception Page when the ASPNETCORE_ENVIRONMENT is set to Development.
    3. Modify your web.config file: If you have a web.config file, you can add an environment variable there. If you don’t have one, you can create one. The environment variable should look like this:
    <aspNetCore processPath="dotnet" arguments=".\\YourProjectName.dll" stdoutLogEnabled="false" stdoutLogFile=".\\logs\\stdout">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
    

    Remember to replace “YourProjectName” with the actual name of your project. Please note that these steps will expose detailed error messages which can include sensitive information, so they should only be used for debugging. Once you’ve finished debugging, remember to set the ASPNETCORE_ENVIRONMENT variable back to “Production” or another appropriate value. This should get you started.

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 59,471 Reputation points
    2024-01-17T17:31:38.1333333+00:00

    development mode will only work if you implemented code to support extra reporting in development mode.

    if (app.Environment.IsDevelopment())

    in later versions of the framework there may be some default support.

    0 comments No comments

  2. Brando Zhang-MSFT 3,281 Reputation points Microsoft Vendor
    2024-01-18T07:07:56.27+00:00

    Hi @Arin Leviti,

    I tried everything to switch environment on my VM but nothing seems to work. I can't swap to Development.

    There is a lot of issue which caused the asp.net core page doesn't work well. To troubleshooting the issue, we need to get the details error message. The reason why you don't get the details error message is the asp.net core contains the codes which disable the details error message when the environment variable is not the development. As the error message said this is due to show the details error message in the production environment is not security. You could find the codes inside program.cs:

    // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }
    

    If you want to see the details error message, you could follow below steps: 1.If you host the application inside the IIS, you could directly find the details error message inside the event viewer's application tag. If you want to enable the development environment variable to see the details error message, you could set below settings inside the web.config.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <location path="." inheritInChildApplications="false">
        <system.webServer>
          <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
          </handlers>
          <aspNetCore processPath="dotnet" 
          arguments=".\WebApplication1.dll" stdoutLogEnabled="false"  stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
          <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
        </environmentVariables>
           </aspNetCore>
        </system.webServer>
      </location>
    </configuration>
    

    2.If you running the application directly by using the cmd. I suggest you could try below codes in cmd: dotnet WebApplication1.dll --environment=Development

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Arin Leviti 60 Reputation points
    2024-01-18T08:33:49.3166667+00:00

    I tried both options and nothing's changed...


  4. Arin Leviti 60 Reputation points
    2024-01-18T12:30:03.6333333+00:00

    I managed to bypass the Environment modifying the Error page like this: but the problem remains. Error Details:

    System.ComponentModel.Win32Exception (2): An error occurred trying to start process 'C:\inetpub\executables\FINAL BATTLE' with working directory 'C:\inetpub\executables'. The system cannot find the file specified.
    
    @page @model MyWebsite.Pages.ErrorModel 
    @{     ViewData["Title"] = "Error"; }  
    <h1 class="text-danger">Error.</h1> <h2 class="text-danger">An error occurred while processing your request.</h2>  
    @if (Model.ShowRequestId) {     
    <p>         <strong>Request ID:</strong> <code>@Model.RequestId</code>     </p> }  
    @if (!string.IsNullOrEmpty(Model.ErrorMessage)) {     
    <h3 class="text-danger">Error Details:</h3>     
    <pre>@Model.ErrorMessage</pre> } 
    
    using System.Diagnostics;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.AspNetCore.Diagnostics;
    
    namespace MyWebsite.Pages
    {
    	[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    	[IgnoreAntiforgeryToken]
    	public class ErrorModel : PageModel
    	{
    		public string ErrorMessage { get; set; }
    		public string RequestId { get; set; }
    		public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
    
    		public void OnGet()
    		{
    			RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
    
    			var exceptionFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
    			if (exceptionFeature != null)
    			{
    				// Retrieve the error message
    				ErrorMessage = exceptionFeature.Error.ToString(); // Using ToString() to get the full exception details
    			}
    		}
    	}
    }