簡體   English   中英

為什么 Asp.Net Core Kestrel 服務器向 Ngrok 返回 404 並且控制器永遠不會被調用?

[英]Why is Asp.Net Core Kestrel server returns 404 to Ngrok and Controller is never gets called?

幾天來,我一直在嘗試運行一個簡單的 asp.net 核心應用程序,並為 Telegram bot 設置 webhook。 它在 Mac 上運行,帶有 Kestrel 服務器和 Ngrok,用於將 https 代理到 webhook。 目前它返回 404 給 Ngrok,我也在控制台中看到 404。 任何幫助表示贊賞。 我已經嘗試了這一行的各種修改./ngrok http http://localhost:5000 -host-header=localhost:5000

我是 Asp.Net Core 的初學者。 這是我的應用程序的核心部分:

[Route("/")]
[ApiController]
public class UpdateController : ControllerBase
{
    [HttpPost]
    public async Task<OkResult> Post([FromBody]Update update)

這個永遠不會被調用。 這是我的 Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    private static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .Build();
}

啟動文件

public class Startup
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        
        Bot.GetBotClientAsync().Wait();
    }
}

機器人

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;
    }
}

設置 webhook 工作正常,Ngrok 收到請求,但來自 Kestrel 的答案始終是 404,並且永遠不會調用 Post 方法。

控制台輸出示例:

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.

Ngrok 檢查選項卡。表明 Ngrok 正在識別服務器。

你發布了所有的 Startup 課程嗎? 我沒有看到任何 ConfigureServices 方法,您必須在 ConfigureServices 方法中使用 services.AddMvc().AddMvcOptions() 行,也沒有看到 app.UseMvc() 行。

我不太確定,但我一直認為您需要這兩行來啟用控制器。

嘗試以這種方式更改您的 Startup 類:

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();
    }
}

@PiGi78 非常感謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM