ASP.NET Core 2.2: web.config and deployment to IIS

0

Big Edits

I have a C# ASP.Net core 2.2 (was 2.1 in original question) web app that I am trying to deploy to an IIS 8.5 server. The deployment happens. I can reach the default route and the server side actions work for that route. None of the other defined routes work though. On the IIS Server, If I try for an MyInitialController action other than Index I get the error page:

    HTTP Error 500.19 - Internal Server Error
    The requested page cannot be accessed because the related configuration data for the page is invalid.

    Detailed Error Information:
    Module     IIS Web Core
    Notification       BeginRequest
    Handler    Not yet determined
    Error Code     0x80070003
    Config Error       Cannot read configuration file
    Config File    \\?\E:\www\myapp.company.com\Content\MyInitialController\web.config
    Requested URL      https://myapp.company.com:443/MyInitialController/Item?device=MY_WIDGET
    Physical Path      E:\www\myapp.company.com\Content\MyInitialController\Calendar
    Logon Method       Not yet determined
    Logon User     Not yet determined

    Config Source:
       -1: 
        0: 

    More Information:
    This error occurs when there is a problem reading the configuration file for the Web server or Web application. In some cases, the event logs may contain more information about what caused this error.
    View more information ยป

Note the value of Config File. On the IIS server there is no folder named: "MyInitialController". The "root" of the IIS site folder is: \?\E:\www\myapp.company.com\Content. I have no idea what is causing it to think that the web.config should be one level down in a folder that does not exist.

startup.cs:

using System;
using CalendarMyInitialControllers.Models.Commodities;
using CalendarMyInitialControllers.Models.UserTracking;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace CalendarMyInitialControllers {
    public class Startup {
        public Startup(IHostingEnvironment env) {
            var builder = new ConfigurationBuilder()
                .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                .AddJsonFile("appsettings.json", true, true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services) {
            services.Configure<CookiePolicyOptions>(options => {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            // Add memory cache services
            services.AddMemoryCache();

            services.AddSession(options => {
                options.IdleTimeout = TimeSpan.FromMinutes(600); //You can set Time   
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
            if (env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
            } else {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseExceptionHandler("/MyInitialController/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseSession();

            app.UseMvc(routes => {
                routes.MapRoute(
                    "default",
                    "{controller=MyInitialController}/{action=Index}"
                );
                routes.MapRoute(
                    "Calendar",
                    "MyInitialController/Item/{device}"
                );
            });
        }
    }
}

launchSettings:

{
    "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
            "applicationUrl": "http://localhost:52839",
            "sslPort": 44375
        }
    },
  "profiles": {
    "IIS Express": {
        "commandName": "IISExpress",
        "launchBrowser": true,
        "applicationUrl": "https://localhost:44375;http://localhost:5000",
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    },
    "CalendarReservations": {
        "commandName": "Project",
        "launchBrowser": true,
        "applicationUrl": "https://localhost:44375;http://localhost:5000",
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    }
  }
}

appSettings:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}
asp.net-core
iis-8
asked on Stack Overflow May 15, 2019 by 7 Reeds • edited May 16, 2019 by 7 Reeds

2 Answers

0

First, be sure to read the Host ASP.NET Core on Windows with IIS. As the guide says in the beginning, make sure you have the hosting bundle installed.

In the guide, it mentions you will need a web.config here.

There is a more detailed guide for web.config here.

When I publish through Visual Studio, one is created for me. If that doesn't happen for you, you can always manually add a web.config at the root of your project. In fact, that's usually a better approach.

A basic example of a web.config would be:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\MyApp.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    </system.webServer>
  </location>
</configuration>
answered on Stack Overflow May 15, 2019 by NeedleInAFullStack
0

I found the unexpected MyInitialController dir issue. The IIS Server Admin created the "Base Settings" -> "Base Path" string as ...myapp.company.com\Content\MyInitialController. I removed the trailing MyInitialController and restarted the site. I now have other issues but this one is closed.

answered on Stack Overflow May 17, 2019 by 7 Reeds

User contributions licensed under CC BY-SA 3.0