ASP.NET Core 2.2 Web API在IIS上托管后出现404错误

3
我创建了一个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>

这个链接可能对您有所帮助:https://dev59.com/BVkT5IYBdhLWcg3wfvin - SUNIL DHAPPADHULE
是的,我已经将它托管在8081端口下。因此,尝试通过http://localhost:8081/访问它。 - Janith Widarshana
它设置为默认网站还是网站下的应用程序?我的意思是,它可能类似于 http://localhost/<应用程序名称>/api/Users - Crowcoder
1
@JanithWidarshana IIS有站点和应用程序的概念。如果您不使用此部署替换默认网站,则它将不会位于localhost:port/api/users,而是位于localhost:port/<application name>/api/users - Crowcoder
1
@JanithWidarshana,如果你将app.UseSwaggerDocumentation();从IF条件中移除,你将在生产环境中拥有swagger文档。请尝试一下是否在IIS中可行。另外,你可以尝试在https://www.getpostman.com/中使用http://localhost:8081/api/users。 - Oshadha
显示剩余5条评论
2个回答

2
以下是您可以检查的一些要点清单:
  1. 根据您的操作系统安装dotnet core版本的windows-hosting-bundle-installer。您可以从下面的链接下载它。
  2. 在IIS中为dotnet core创建一个新的应用程序池,您可以查看下面的图像以获取设置enter image description here
  3. 针对所有托管的内容,将任何与dotnet core相关的应用程序定位到新创建的应用程序池。
请查看上述是否解决了问题。如果有任何疑问,请回复。 如果您的问题已解决,请投票并点赞,这可能会对他人有所帮助。

你能分享一下你的控制器代码以便更好地理解吗? - Pabitro
更新了控制器类的问题。我刚刚从草图中创建了一个 .net core 2.2 web api 项目,并没有改变任何内容然后尝试发布它。但仍有相同的问题。请问有人能够尝试一下吗? - Janith Widarshana

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接