在IIS上托管后,ASP.NET Core 2.2 Web API给出404

时间:2019-05-28 09:32:05

标签: c# asp.net asp.net-core asp.net-core-webapi

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

我启用了swagger文档,也使用了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>

2 个答案:

答案 0 :(得分:2)

下面是一些您可以检查是否已完成的清单。

  1. 为您的OS安装dotnet核心版本的Windows-hosting-bundle-installer。您可以从下面的link
  2. 下载它
  3. 在IIS中为dotnet core创建一个新的应用程序池,您可以在下面的图像中查看设置enter image description here
  4. 针对所有托管,将与dotnet core相关的任何应用程序定位到新创建的应用程序池。

查看以上是否解决了问题。恢复任何查询 投票,并说您的问题是否得到解决,以便对某人有所帮助。

答案 1 :(得分:1)

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

  1. 启用IIS托管Web核心

    enter image description here

  2. 安装“ Visual Studio 2015可再发行的Visual C ++”

    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应用程序池

    enter image description here

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