Web 部署 ASP.Net Core 到 IIS 后出现开发模式错误

Development Mode Error after web deploying ASP.Net Core to ISS

我将 ASP.NET 核心应用程序 (v5) 部署到本地 ISS,但是当我尝试访问我的一个 API 控制器时,我认为 link:http://my.website/api/controllersapi.我收到以下错误

我已经有了 Development 的环境变量,所以我不确定我还应该做什么。 这是我的应用程序中的一些 类,因此您可以了解它的配置方式。

Program.cs:

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();

                   webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
                    webBuilder.UseIISIntegration();
                });
    }

StartUp.cs:

public void ConfigureServices(IServiceCollection services)
        {

            services.AddDbContext<AutoPatchManagementContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("AutoPatchManagementContext")));

            services.AddSignalR();
            services.AddControllersWithViews().AddNewtonsoftJson(options =>
               options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
           );

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment 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.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();
           
            app.UseEndpoints(endpoints =>
            {
                
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Homepage}/{id?}");
                endpoints.MapHub<MyHub>("/myHub");
            });
        }

launchSetting.json

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

这是我的 IISProfile.pubxml

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish>http://my.website</SiteUrlToLaunchAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <ProjectGuid>14712f10-a37a-441a-ad5e-e37f85eff5f4</ProjectGuid>
    <SelfContained>false</SelfContained>
    <MSDeployServiceURL>localhost</MSDeployServiceURL>
    <DeployIisAppPath>my.website</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>InProc</MSDeployPublishMethod>
    <EnableMSDeployBackup>False</EnableMSDeployBackup>
    <EnableMsDeployAppOffline>True</EnableMsDeployAppOffline>
    <TargetFramework>net5</TargetFramework>
  </PropertyGroup>
</Project>

我已经搜索过这个错误,我找到的唯一解决方案是将 de enviorment 变量更改为 Development,这已经是。我当前的配置有问题吗?

提前致谢

您需要在 web.config 中添加开发,而不是任何其他文件。 Web.config 应该是这样的:

<aspNetCore processPath="dotnet" arguments=".\TestWebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
   <environmentVariables>
       <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
   </environmentVariables>
</aspNetCore>