我创建了一个 ASP.Net Core 2.2 Web Api 项目,它在本地运行没有任何问题。在我将它发布到文件系统后,它总是给我 404 问题。我启用了与 IIS 和 asp.net 框架 web api2 应用程序相关的 Windows features 在同一台服务器上运行良好。
我启用了 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>
请您参考如下方法:
下面是一些 list ,您可以检查自己是否已经完成。
- 为您的操作系统安装 dotnet 核心版本的 windows-hosting-bundle-installer。可以在下面下载link
- 在您的 IIS 中为 dotnet 核心创建一个新的应用程序池,您可以查看下图的设置
- 将与 dotnet 核心相关的任何应用程序定位到新创建的应用程序池,以进行所有托管。
查看以上是否解决了问题。回复任何查询 如果您的问题得到解决,请投票并点赞,这样它可能会对某人有所帮助。
