繁体   English   中英

ASP.NET Core 2.2 Web API在IIS上托管后提供404

[英]ASP.NET Core 2.2 Web API gives 404 after hosting on IIS

我创建了一个ASP.Net Core 2.2 Web Api项目,它在本地运行没有任何问题。 我将它发布到文件系统后,总是给我404问题。 我启用了与IIS相关的Windows文件,asp.net框架web api2应用程序在同一台服务器上运行良好。

我已经启用了swagger doc并且也使用了Microsoft.AspNetCore.Authentication库。

Program.cs中

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace US.BOX.AuthAPI
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}

Startup.cs

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using US.BOX.AuthAPI.Extensions;

namespace US.BOX.AuthAPI
{
    public class Startup
    {
        private readonly IConfiguration _configuration;
        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        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<IISOptions>(options =>
            {
                options.ForwardClientCertificate = false;
            });

            services.Configure<ApiBehaviorOptions>(options =>
            {
                options.SuppressModelStateInvalidFilter = true;
            });

            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton<IAuthenticationSchemeProvider, CustomAuthenticationSchemeProvider>();

            services.AddSwaggerDocumentation();
            services.AddJwtBearerAuthentication(_configuration);

            services.AddCors();
            services.AddLogging();

            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ILogger<Startup> logger)
        {
            app.UseAuthentication();

            if (env.IsDevelopment())
            {
                app.UseSwaggerDocumentation();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}

appsettings.json

{
  "JWT": {
    // TODO: This should be updated for production deployment
    "SecurityKey": "sDIkdjhkalUthsaCVjsdfiskokrge",
    "Issuer": "https://{host_name}:{port}",
    "Audience": "https://{host_name}:{port}",
    "ExpirationTimeInMinutes": 60
  },
  "Logging": {
    "LogFilePath": "Logs/auth-{Date}.txt",
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

UsersController.cs

using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace US.BOX.AuthAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetAll()
        {
            try
            {
                return Ok("Users");
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}

发布后,生成以下web.config文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\US.BOX.AuthAPI.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
    </system.webServer>
  </location>
</configuration>

以下是您可以检查的一些检查清单。

  1. 为你的操作系统安装windows-hosting-bundle-installer为你的dotnet核心版本。 你可以从以下链接下载它
  2. 在您的IIS中为dotnet核心创建一个新的应用程序池,您可以检查下面的图像以获取设置 在此输入图像描述
  3. 针对所有托管,将与dotnet核心相关的任何应用程序定位到新创建的应用程序池。

看看上面是否解决了这个问题。 恢复任何查询投票,如果你的问题得到解决,以便它可以帮助某人。

希望这会对某人有所帮助。 以下是我为解决此问题所采取的步骤。

  1. 启用IIS Hostable Web Core

    在此输入图像描述

  2. 安装'Visual C ++ Redistributable for Visual Studio 2015'

    https://www.microsoft.com/en-us/download/details.aspx?id=48145

  3. 安装'dotnet-hosting-2.2.6-win.exe'

    https://dotnet.microsoft.com/download

  4. 在.NET CLR版本中使用“无管理代码”创建单独的IIS应用程序池

    在此输入图像描述

**注意执行上述步骤的顺序非常重要

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM