Asp.net core ASP.NET Core 2.2:web.config和部署到IIS

Asp.net core ASP.NET Core 2.2:web.config和部署到IIS,asp.net-core,iis-8,Asp.net Core,Iis 8,大编辑 我有一个C#ASP.NETCore2.2(最初的问题是2.1)web应用程序,我正试图将其部署到IIS8.5服务器上。部署发生了。我可以到达默认路由,并且服务器端操作适用于该路由。但是,其他定义的路由都不起作用。在IIS服务器上,如果我尝试执行MyInitialController操作而不是Index,则会出现错误页面: HTTP Error 500.19 - Internal Server Error The requested page cannot be acces

大编辑

我有一个C#ASP.NETCore2.2(最初的问题是2.1)web应用程序,我正试图将其部署到IIS8.5服务器上。部署发生了。我可以到达默认路由,并且服务器端操作适用于该路由。但是,其他定义的路由都不起作用。在IIS服务器上,如果我尝试执行
MyInitialController
操作而不是
Index
,则会出现错误页面:

    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 »
请注意
配置文件的值。IIS服务器上没有名为“MyInitialController”的文件夹。IIS站点文件夹的“根目录”是:\?\E:\www\myapp.company.com\Content。我不知道是什么导致它认为web.config应该在一个不存在的文件夹中降低一级

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}"
                );
            });
        }
    }
}
应用程序设置:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

首先,一定要阅读文章。正如指南在开头所说的,请确保已安装托管捆绑包

在指南中,它提到您将需要一个
web.config

关于
web.config
,有更详细的指南

当我通过VisualStudio发布时,会为我创建一个。如果您没有遇到这种情况,则始终可以在项目的根目录下手动添加web.config。事实上,这通常是一个更好的方法

web.config
的一个基本示例是:

<?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>


我发现了意外的
MyInitialController
dir问题。IIS服务器管理员将“基本设置”->“基本路径”字符串创建为
…myapp.company.com\Content\MyInitialController
。我删除了尾随的
MyInitialController
,并重新启动了站点。我现在有其他问题,但此问题已解决。

运行报告以显示更多信息。您可以将报告粘贴为问题的一部分,以便其他人可以对可能出现的错误进行评论。我们将在明天进行讨论。我不知道有这样的事存在
<?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>