90

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.

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
nam
  • 21,967
  • 37
  • 158
  • 332
  • 5
    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" – Luis Palacios Sep 28 '16 at 14:09
  • 2
    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 – Luis Palacios Sep 28 '16 at 14:11
  • Solved environment issue via publish profile, see https://stackoverflow.com/a/64368721/1662459 – G J Oct 15 '20 at 12:44
  • you control the environment variable from the web.config file in the EnvironmentVariables xml block with – Golden Lion Aug 04 '21 at 16:10

15 Answers15

62

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.

Community
  • 1
  • 1
Sanket
  • 19,295
  • 10
  • 71
  • 82
  • Thank you for trying to help. In ` – nam Sep 28 '16 at 16:19
  • 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. – Sanket Sep 28 '16 at 16:28
  • 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? – nam Sep 28 '16 at 18:10
  • 1
    Try IIS reset. Also try with ASPNETCORE_ENVIRONMENT = Development and see what happens. – Sanket Sep 28 '16 at 18:18
  • @Sunket IISReset did not help. Then I changed to `ASPNETCORE_ENVIRONMENT = Development ` that gave me following error without starting the home page. On the other hand if I use `Production` in the variable value it does start the home page but gives me the original error\info message I have in my post above after I enter login\password info and hit enter on the login page. **Error**: `500 Internal Server Error System.InvalidOperationException Unable to locate a project.json at 'C:\DotNet2015\PublishToIIS\DeploymentTest\'.` – nam Sep 28 '16 at 18:57
  • Not sure why you are getting error for project.json. You don't need project.json on publish application. Try to debug from VS2015 and see where exactly this is error occurs. You can set ASPNETCORE_ENVIRONMENT variable in project properties > Debug tab. – Sanket Sep 29 '16 at 03:02
  • The issue seem to be related to Database since the home page displays fine but on the login page when app hits the database I get the meessage that (as mentioned by @LuisPalacios in his second comment above) is not an error message its rather an info mesage. The message may be misleading since I had published another MVC Core app to IIS with no problem but in that App I had added `ApplicationPoolIdentity` account to the SQL Db. I may have to do the same for the app in question. However, the absence of this account does not cause any issue when app is running in VS (with IIS Express). – nam Sep 29 '16 at 17:21
  • @Sanket Thanks, had the problem on a production server, I was able to quickly switch to Dev mode and debug, goig back to Production mode just after – Christian Navelot Nov 06 '17 at 12:29
  • 3
    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. – cdonner Aug 05 '20 at 17:57
48

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

Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28
Deep
  • 1,025
  • 11
  • 7
29

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

Abdulhakim Zeinu
  • 3,333
  • 1
  • 30
  • 37
24

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.

Ani
  • 431
  • 3
  • 7
  • This totally helped me with an Azure problem I was having...ended up needing to add a firewall rule on the master database. – Chris Catignani Jan 08 '21 at 23:18
  • this post deserves more attention, this is also the method i used to get over that developer page problem – Ji_in_coding Mar 04 '21 at 07:07
  • Its 2023 and this response helped me figure out how to see my real error. Also, because of your demonstration of using an App Pool LOGIN to associate with a SQL user I realized I could just use Windows Integrated Security on both my development machine (as myself) and then as the IIS AppPool user in production. In my case I didn't give the SQL user 'db_owner' permissions, but rather just 'db_write' and 'db_read' as I don't want my web app creating/managing db objects. Thanks again! – prilldev Aug 04 '23 at 17:15
5

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.

Aminur Rahman
  • 400
  • 1
  • 6
  • 14
4

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();
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
مهدی
  • 333
  • 3
  • 6
3

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;
Janaka
  • 398
  • 5
  • 16
2

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

Jared Tims
  • 191
  • 1
  • 1
  • 9
2

This error message is just a hard-coded text in the Error.cshtml file, not the actual error message, only the RequestID is dynamically generated, but not helpful. It only comes up when you publish it to a production web server, and if there is an error.

The template wizard adds an Error.cshtml and Error.cshtml.cs files to the Pages folder if the project is a Razor Pages while it only adds an Error.cshtml to the Views\Shared folder if the project is MVC. This file was there since ASP.NET Core 2.0, still unchanged.

Code in the Error.cshtml file as follows (ASP.NET Core Razor Pages project):

@page
@model 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>
  <p>@Model.</p>
}

<h3>Development Mode</h3>
<p>
   Swapping to the <strong>Development</strong> environment displays detailed 
  information about the error that occurred.
</p>
<p>
    <strong>The Development environment shouldn't be enabled for deployed applications.</strong>
    It can result in displaying sensitive information from exceptions to end users.
    For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
    and restarting the app.
</p>

In the MVC project, the code is the same except first 2 lines, it has only one line, replacing ErrorModel with ErrorViewModel:

@model ErrorViewModel

If there is an error this file will show if the environment is NOT the DEVELOPMENT, while it will show the actual error message in the development environment, based on the code in the Configure method in Startup.cs file. The code below shows for Razor Pages, for MVC only change is the path to the Error file app.UseExceptionHandler("/Home/Error");:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseDatabaseErrorPage();
}
else
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

We do not need to change the above code. So this Development Mode message will come up for any error pops from the application. If you want to show the proper error messages, More information can be found here in the doc.

To avoid confusion, change the original message as shown above, <h3> header and <p> to:

<h3>This is Production Mode  </h3>
<p>Contact the developers of the app. If you are the developer swap to the
 Development environment to see detailed information about the error that occurred. 
</p>

Now if we take a look at the line with <aspNetCore in the Web.config file in your server:

<aspNetCore processPath=".\OurASPNETCoreApplication.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />

That indicates that this is in production mode by default since it doesn't include the child <environmentVariables> node. Now to change the environment to development change that line to:

<aspNetCore processPath=".\OurASPNETCoreApplication.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" >
    <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
    </environmentVariables>
</aspNetCore>

and restart the web server. It will show the actual error message.

Most of the time it will be the database configuration errors.

Alternatively use a third-party logging provider, like Serilog to write logs into a text file.

Dush
  • 1,185
  • 26
  • 29
1

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

Nick
  • 138,499
  • 22
  • 57
  • 95
Med EC
  • 21
  • 1
  • 7
1

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!

francoisr
  • 4,407
  • 1
  • 28
  • 48
JWallace
  • 487
  • 4
  • 7
1

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.

Judy M
  • 11
  • 2
0

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.

Dan Friedman
  • 4,941
  • 2
  • 41
  • 65
0

This is an old thread. I didn't find the answer here. But I found a solution and want to share it.

All worked perfectly on my computer. But when I publish to the web server and open the new page "MyLogin" I get the error. Solution that worked for me:

Delete the "bin" and "obj" folder in the current Project.

Remove all files/folders on the destination folder on the web server.

Skak2000
  • 86
  • 1
  • 4
-2

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

Syed Nasir Abbas
  • 1,722
  • 17
  • 11