c# - ASP.NET Core 2.2 Web API 在 IIS 上托管后给出 404
问题描述
我创建了一个 ASP.Net Core 2.2 Web Api 项目,它在本地运行没有任何问题。在我将它发布到文件系统后,它总是给我 404 问题。我已经启用了与 IIS 相关的 windows 功能,并且 asp.net 框架 web api2 应用程序在同一台服务器上运行良好。
我启用了 swagger doc 并且也使用了 Microsoft.AspNetCore.Authentication 库。
程序.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>();
}
}
启动.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();
}
}
}
应用设置.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": "*"
}
用户控制器.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>
解决方案
以下是一些您可以检查是否已完成的清单。
- 为您的操作系统的 dotnet 核心版本安装 windows-hosting-bundle-installer。你可以从下面的链接下载它
- 在 IIS 中为 dotnet core 创建一个新的应用程序池,您可以查看下图的设置
- 将与 dotnet core 相关的任何应用程序定位到新创建的应用程序池,用于所有托管。
看看上面是否解决了问题。如果您的问题得到解决,请回复任何查询投票并喜欢它,以便它可以帮助某人。
推荐阅读
- grpc - grpc-web TLS 是否需要客户端的任何配置?
- xml - XSLT 兄弟递归按列跨度分组列
- api - Google Analytics Reporting API 维度 广告内容 ID
- ios - react-native-ios xcode Capabilities for Signing & Capabilities 可能无法正常运行,因为它的权利使用占位符团队 ID
- python-2.7 - PYQT5 连接两个 QSpinBox
- java - 在 Java 中获取接口的类型
- python - 构建我的 Python 包以便导入语句正常工作
- python - 如何将元组中的所有值添加到字典中?
- react-navigation - 在 React Navigation 中使用 iOS 13 样式的模式时如何修改“深”背景?
- laravel - 同步不同数据库表的最佳方法