C# 为什么Asp.Net核心Kestrel服务器将404返回给Ngrok,而控制器从未被调用?
几天来,我一直在尝试运行一个简单的asp.net核心应用程序,并为Telegrame bot安装webhook。它在Mac上运行,有Kestrel服务器,Ngrok在上面代理https到webhook。目前,它返回404到Ngrok,我也在控制台中看到404。感谢您的帮助。 我试过对这一行进行各种修改C# 为什么Asp.Net核心Kestrel服务器将404返回给Ngrok,而控制器从未被调用?,c#,asp.net,asp.net-core,kestrel-http-server,ngrok,C#,Asp.net,Asp.net Core,Kestrel Http Server,Ngrok,几天来,我一直在尝试运行一个简单的asp.net核心应用程序,并为Telegrame bot安装webhook。它在Mac上运行,有Kestrel服务器,Ngrok在上面代理https到webhook。目前,它返回404到Ngrok,我也在控制台中看到404。感谢您的帮助。 我试过对这一行进行各种修改 /ngrok-httphttp://localhost:5000 -主机头=本地主机:5000 我是Asp.Net核心的初学者。以下是我的应用程序的核心部分: [Route("/&quo
/ngrok-httphttp://localhost:5000 -主机头=本地主机:5000
我是Asp.Net核心的初学者。以下是我的应用程序的核心部分:
[Route("/")]
[ApiController]
public class UpdateController : ControllerBase
{
[HttpPost]
public async Task<OkResult> Post([FromBody]Update update)
Bot.cs
public static class Bot
{
private static TelegramBotClient botClient;
private static List<Command> commandsList;
public static IReadOnlyList<Command> Commands => commandsList.AsReadOnly();
public static async Task<TelegramBotClient> GetBotClientAsync()
{
if (botClient != null)
{
return botClient;
}
commandsList = new List<Command>
{
new StartCommand()
};
botClient = new TelegramBotClient(AppSettings.Token);
await botClient.SetWebhookAsync(AppSettings.NGrokUrl);
return botClient;
}
}
你把你所有的创业课程都贴出来了吗? 我没有看到任何ConfigureServices方法必须有services.AddMvc().addmvcopions()行,也没有看到Configure方法中的app.UseMvc()行 我不太确定,但我一直认为您需要这两条线来启用控制器 尝试以以下方式更改启动类:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddMvcOptions();
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseMvc();
Bot.GetBotClientAsync().Wait();
}
}
经过对@PiGi78答案的一些修改,它确实起了作用。以下是最终解决方案:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddMvcOptions(ApplyMvcOptions);
services.AddControllersWithViews();
services.AddControllers().AddNewtonsoftJson();
}
private void ApplyMvcOptions(MvcOptions options)
{
options.EnableEndpointRouting = false;
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseMvc();
Bot.GetBotClientAsync().Wait();
}
}
@非常感谢你 它确实有用!非常感谢。但是我不得不在代码中添加几行,因此我发布了一个新的答案。
dbug: Microsoft.AspNetCore.Hosting.Diagnostics[3]
Hosting starting
dbug: Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer[2]
Failed to locate the development https certificate at '(null)'.
dbug: Microsoft.AspNetCore.Hosting.Diagnostics[4]
Hosting started
dbug: Microsoft.AspNetCore.Hosting.Diagnostics[0]
Loaded hosting startup assembly eBot
Hosting environment: Development
Content root path: /Users/yuriibabii/Projects/eBot/eBot
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[6]
Connection id "0HM4GMVPJLPSF" received FIN.
dbug: Microsoft.AspNetCore.Server.Kestrel[39]
Connection id "0HM4GMVPJLPSF" accepted.
dbug: Microsoft.AspNetCore.Server.Kestrel[1]
Connection id "0HM4GMVPJLPSF" started.
dbug: Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[7]
Connection id "0HM4GMVPJLPSF" sending FIN because: "The client closed the connection."
dbug: Microsoft.AspNetCore.Server.Kestrel[10]
Connection id "0HM4GMVPJLPSF" disconnecting.
dbug: Microsoft.AspNetCore.Server.Kestrel[2]
Connection id "0HM4GMVPJLPSF" stopped.
dbug: Microsoft.AspNetCore.Server.Kestrel[39]
Connection id "0HM4GMVPJLPSG" accepted.
dbug: Microsoft.AspNetCore.Server.Kestrel[1]
Connection id "0HM4GMVPJLPSG" started.
dbug: Microsoft.AspNetCore.Server.Kestrel[39]
Connection id "0HM4GMVPJLPSH" accepted.
dbug: Microsoft.AspNetCore.Server.Kestrel[1]
Connection id "0HM4GMVPJLPSH" started.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 POST http://localhost:5000/ application/json 375
dbug: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
Wildcard detected, all requests with hosts will be allowed.
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
Connection id "0HM4GMVPJLPSH" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 45.4537ms 404
dbug: Microsoft.AspNetCore.Server.Kestrel[25]
Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000001": started reading request body.
dbug: Microsoft.AspNetCore.Server.Kestrel[26]
Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000001": done reading request body.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 GET http://localhost:5000/
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
Connection id "0HM4GMVPJLPSG" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 9.3331ms 404
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 POST http://localhost:5000/ application/json 375
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
Connection id "0HM4GMVPJLPSH" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 10.7676ms 404
dbug: Microsoft.AspNetCore.Server.Kestrel[25]
Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000002": started reading request body.
dbug: Microsoft.AspNetCore.Server.Kestrel[26]
Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000002": done reading request body.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 POST http://localhost:5000/ application/json 375
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
Connection id "0HM4GMVPJLPSH" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 8.5738ms 404
dbug: Microsoft.AspNetCore.Server.Kestrel[25]
Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000003": started reading request body.
dbug: Microsoft.AspNetCore.Server.Kestrel[26]
Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000003": done reading request body.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 POST http://localhost:5000/ application/json 375
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
Connection id "0HM4GMVPJLPSH" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 8.8111ms 404
dbug: Microsoft.AspNetCore.Server.Kestrel[25]
Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000004": started reading request body.
dbug: Microsoft.AspNetCore.Server.Kestrel[26]
Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000004": done reading request body.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 POST http://localhost:5000/ application/json 375
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[2]
All hosts are allowed.
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
Connection id "0HM4GMVPJLPSH" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 9.6591ms 404
dbug: Microsoft.AspNetCore.Server.Kestrel[25]
Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000005": started reading request body.
dbug: Microsoft.AspNetCore.Server.Kestrel[26]
Connection id "0HM4GMVPJLPSH", Request id "0HM4GMVPJLPSH:00000005": done reading request body.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddMvcOptions();
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseMvc();
Bot.GetBotClientAsync().Wait();
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddMvcOptions(ApplyMvcOptions);
services.AddControllersWithViews();
services.AddControllers().AddNewtonsoftJson();
}
private void ApplyMvcOptions(MvcOptions options)
{
options.EnableEndpointRouting = false;
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseMvc();
Bot.GetBotClientAsync().Wait();
}
}