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

后端 未结 8 2149
一个人的身影
一个人的身影 2020-12-02 20:05

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

相关标签:
8条回答
  • 2020-12-02 20:38

    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.

    0 讨论(0)
  • 2020-12-02 20:41

    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.

    0 讨论(0)
  • 2020-12-02 20:43

    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.

    0 讨论(0)
  • 2020-12-02 20:46

    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.

    0 讨论(0)
  • 2020-12-02 20:54

    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

    0 讨论(0)
  • 2020-12-02 20:55

    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>
    

    0 讨论(0)
提交回复
热议问题