ChatGPT解决这个技术问题 Extra ChatGPT

ASP.NET Core deployment to IIS error: Development environment should not be enabled in deployed applications

I followed this article to deploy my ASP.NET MVC Core 1.0 app to local IIS on my Windows 10 that is using IIS 10. The application deployed successfully and it opens the home page fine. I'm using Individual User Accounts Authentication. On the home page when I enter login/password and click Login button, I get the following error. I'm using the latest versions of ASP.NET Core and VS2015. I used VS2015 Publish wizard to publish the app. Everything is done on the same machine:

An error occurred while processing your request. Development Mode Swapping to Development environment will display more detailed information about the error that occurred. Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development, and restarting the application.

When you publish an asp net core by default if the ASPNETCORE_ENVIRONMENT variable is not set it will act as production, you could try as the message sugest try to set the ASPNETCORE_ENVIRONMENT in the environment variables or in the web config to Development temporally to debug what is going on, if you are using a appsettings.development.json in production it won't be read if the enviremoent variable is not set to "Development"
And btw this Message: "Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development, and restarting the application." is not the error is just a warning/suggestion but you could set it to development even when is deployed
Solved environment issue via publish profile, see stackoverflow.com/a/64368721/1662459
you control the environment variable from the web.config file in the EnvironmentVariables xml block with

C
Community

First, check the value of ASPNETCORE_ENVIRONMENT variable. You will have to set this environment variable to "Production" (or other environment than Development)

Otherwise, you can update web.config like this-

<configuration>
  <!--
    Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
  -->
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath=".\Application.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>

Refer this post for more details.


Thank you for trying to help. In <aspNetCore processPath=".\Application.exe" arguments="" part of your suggestion, 1. Is the Application the name of the project or just as it is? 2. Does arguments value need to be empty?
1. Yes, Application is name of project 2. arguments are nothing but command line arguments. If you are expecting any then you can pass here otherwise leave it empty.
I used your web.config file you posted above (after replacing Application with my project name) but I'm still getting the same error\info message shown in my post above. Any ideas\suggestions?
Try IIS reset. Also try with ASPNETCORE_ENVIRONMENT = Development and see what happens.
I don't get why this backwards answer has 30 upvotes. The message is telling him to switch to Development to see more debug information, not to Production.
M
Mustapha Larhrouch

I wanted to run it in development environment, so I added following in web.config file, and it worked for me:

<environmentVariables>
     <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>

https://i.stack.imgur.com/GqIGm.png


A
Abdulhakim Zeinu

If you are developing using ASP.NET CORE. You can find this setting inside properties and then in launchSetting.json file.

"profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      },
      "nativeDebugging": false
    },
    "Ecommerce": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }

Change "ASPNETCORE_ENVIRONMENT": "Development" to "ASPNETCORE_ENVIRONMENT": "Production"

You can find the launchSetting.json file by expanding properties

https://i.stack.imgur.com/g9HtS.png


Where is the launch setting.json file?
I have edited the answer by adding image that shows the file
@IlludiumPu36 Are you getting this error through out all project or on some functions?
Throughout...see this site, error appears from the top menus parakeetbay.com/Students
@IlludiumPu36 Some times this kind of error occurs when there is a reference to local resources after deploying your project. E.g DB connection string, images and other types of assets. There should not be any kind of local(local directory) references on deployed applications.
A
Ani

I had the same problem (ASP.NET CORE 3.1) but changing "ASPNETCORE_ENVIRONMENT" did not helped.

Scouring through the web I found that in Startup.cs, Configure method, this code was hiding the real issue.

 if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

Then I deleted the If block and added Database error pages ( You might need to Install Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore from NuGet )

app.UseDatabserrorPages();

So your Startup.cs will look like this

app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
            
app.UseHttpsRedirection();

//Others will be Okay

Then you will see the real errors on the webpage. For me it was

Login failed for user IIS APPPOOL\DefaultAppPool

So I had to run a GRANT SCRIPT. I just had to run this script on my SQL Server

IF NOT EXISTS (SELECT name FROM sys.server_principals WHERE name = 'IIS APPPOOL\DefaultAppPool')
BEGIN
    CREATE LOGIN [IIS APPPOOL\DefaultAppPool] 
      FROM WINDOWS WITH DEFAULT_DATABASE=[master], 
      DEFAULT_LANGUAGE=[us_english]
END
GO
CREATE USER [WebDatabaseUser] 
  FOR LOGIN [IIS APPPOOL\DefaultAppPool]
GO
EXEC sp_addrolemember 'db_owner', 'WebDatabaseUser'
GO

You can see this link : https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/deploying-to-iis

And my problem was solved. Hope this helps somebody.


This totally helped me with an Azure problem I was having...ended up needing to add a firewall rule on the master database.
this post deserves more attention, this is also the method i used to get over that developer page problem
A
Aminur Rahman

There is a runtime exception in code. in Production mode it can not be show. so that it show "Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users"

in web.config file you will find

<aspNetCore processPath="dotnet" arguments=".\PortfolioApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />

replace it with

<aspNetCore processPath=".\Application.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
</aspNetCore>

now you run app in browser. It will show actual error message. Now it's time to fix the runtime exception.


A
Adrian Mole

I just replaced this:

    if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseMigrationsEndPoint();
        }
    else
        {
            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();
        }

with this:

     app.UseDeveloperExceptionPage();
     app.UseMigrationsEndPoint();

N
Nick

First, this error occurs where you publish a web site that raises errors in the run-time. So check your code again in the pages that give this error. Then, set the value of ASPNETCORE_ENVIRONMENT variable to Production (instead of Development), you should also check the layout page and change <environment"development"> to <environment"Production">. Finally, publish your web site. This is tested in VS2017


f
francoisr

This might not be the case for everyone, however I was trying to deploy a "release" configuration to a server that had an environment variable of "uat". I set up a uat configuration to use with my deployment and the message no longer appeared when navigating to my site url. Long story short, just make sure your intended build configuration matches the destination server as others have alluded to above!


J
Judy M

The only way I could get rid of the Development Mode message was to change appsettings.json context from Integrated Security=True to specifying User Id=username;Password=password and making sure the user was a db_owner.


You are right, it said "Login failed for user 'IIS APPPOOL\XXXXX'" which I need to add a new login for it
J
Janaka

When your connection string is not correct, you get this error. When I correct my connection string it worked fine.

Eg: for correct azure db connection string

Server={Server Name};Initial Catalog={Database Name};Persist Security Info=False;User ID={DB User Name};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

D
Dan Friedman

By default, in production, you will see this error page unless you create/customize your own. Depending on the project type, it can be in different places like Pages/Error.razor or as a controller action.


J
Jared Tims

For me, it was a matter of adding the EnvironmentName property to the pubxml.

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-5.0


S
Syed Nasir Abbas

This is the default error page with generic error message.

I got this error after deploying the ASP.NET Core 3.1 MC published application on shared hosting. Default Home and Privacy pages were working as expected but when I tried to open a page that was fetching data from database the above error shown.

Error reason: In appsettings.json, I updated connection string Data Source=MyPC\MSSQLSERVER14 with Data Source=.\MSSQLSERVER2. I copied this data source ".\MSSQLSERVER2" from shared hosting connection string and pasted it in appsettings.json

Resolution: Changed data source ".\MSSQLSERVER2" to ".\\MSSQLSERVER2".

"ConnectionStrings": { "AppCon": "Data Source=.\\MSSQLSERVER2xxx;Initial Catalog=sqldb;Persist Security Info=True;User ID=sqluser;Password=********" }