我有一个控制台应用程序,是用 .NET Core 2.2.6 编写的,使用 Kestrel 来托管一个简单的 WebApi。
如果我发布代码并运行可执行文件,我可以访问http://127.0.0.1:310/settings并看到预期的“Hello world! controller”。然而,如果我在Visual Studio 2019中进行调试(甚至在发布模式下打开),相同的URL会抛出404异常。 一些其他可能有助于确定问题的代码:
public class SettingsController : Controller
{
//
// GET: /settings/
public string Index()
{
return $"Hello world! controller";
}
}
如果我发布代码并运行可执行文件,我可以访问http://127.0.0.1:310/settings并看到预期的“Hello world! controller”。然而,如果我在Visual Studio 2019中进行调试(甚至在发布模式下打开),相同的URL会抛出404异常。 一些其他可能有助于确定问题的代码:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureKestrel((context, options) =>
{
options.ListenAnyIP(310, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1;
});
})
.UseStartup<Startup>();
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDefaultFiles(new DefaultFilesOptions()
{
DefaultFileNames = new List<string>() { "index.html" }
});
// Return static files and end the pipeline.
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
const int durationInSeconds = 60 * 60 * 24;
ctx.Context.Response.Headers[HeaderNames.CacheControl] =
"public,max-age=" + durationInSeconds;
}
});
// Use Cookie Policy Middleware to conform to EU General Data
// Protection Regulation (GDPR) regulations.
app.UseCookiePolicy();
// Add MVC to the request pipeline.
app.UseMvcWithDefaultRoute();
}
}
options.ListenAnyIP(310, listenOptions =>
)。与Web服务器的连接已经建立,因为我收到了404错误,并且我可以看到控制台记录Kestrel接收到请求但找不到文件的信息。 - Mark Cilia Vincenti