NET 将核心部署到 IIS 错误: 不应在已部署的应用程序中启用开发环境

我跟随 这篇文章将我的 ASP.NET MVC Core 1.0应用程序部署到使用 IIS 10的 Windows 10上的本地 IIS。应用程序成功部署,并打开了主页。我用的是 个人用户帐户认证。在主页上,当我输入 login/password 并单击 登陆按钮时,会得到以下错误。我使用的是 ASP.NET Core 和 VS2015的最新版本。我使用 VS2015发布向导来发布应用程序。一切都在同一台机器上完成:

处理请求时出错。

发展模式

切换到 发展环境将显示有关所发生错误的更详细信息。
开发环境不应该在已部署的应用程序 中启用,因为它可能导致将异常的敏感信息显示给最终用户。对于本地调试,可以通过将 ASPNETCORE _ Environment环境变量设置为 发展并重新启动应用程序来启用开发环境。

170459 次浏览

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.

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>

enter image description here

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

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!

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

enter image description here

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.

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.

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://learn.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.

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.

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();

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;

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

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

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=********" }