簡體   English   中英

為什么我的 C#.Net Core Rest API 路由找不到我的方法?

[英]Why isn't my C# .Net Core Rest API route finding my method?

我正在研究 API。 我有一個“AbstractController.cs”,並且在調用帶有兩個參數的 GET 時遇到了困難。

[Route("api/[controller]")]
[ApiController]
public class AbstractController : ControllerBase
{
    // GET: api/Abstract
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "missing implementation" };
    }

轉到 https://localhost:44363/api/abstract/ 會生成這個。 [“缺少實現”]太棒了!

現在我需要制作在演出年份傳遞的 Get 方法,並將代碼寫入 SQL 查詢。 夠容易嗎?

// GET: api/Abstract?ShowYear=2019&ShowCode=248621
    [Route("api/{controller}/{ShowYear}/{ShowCode}")]
    [HttpGet]
    public Abstract Get(int ShowYear, int ShowCode) // GetAbstractByYearAndSHCode
    {
        string x = "";
        return new Abstract();
    }

無論我做什么,我都無法讓這個方法斷點/進入執行,我猜這是一個路由問題。 但我嘗試了最可行的方式來調用端點。

在此處輸入圖像描述

現在我像任何自尊學習程序員一樣檢查 MS 文檔。 https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1

ASP.NET Core 3.0 及更高版本中的端點路由:

沒有路線的概念。 不為可擴展性的執行提供排序保證,所有端點都會立即處理。

GULP聽起來很嚇人。

在這一點上,我還沒有觸及我的中間件。

我看過的所有示例都沒有我的“MapControllers();” Startup.cs 中的方法。 這是因為它是 .NET Core 3.0/3.1 的新方法:“ 將 controller 操作的端點添加到 IEndpointRouteBuilder 而不指定任何路由。

好的,那我這里還要手動指定路線嗎? 我這樣做了,它有點工作。

好吧,當我從 go 到 https://localhost:44363/api/abstract/2019/287 時,它現在在 startup.cs 中出現斷點在此處輸入圖像描述

等等,我的 controller 代碼沒有做任何事情? 為什么?

以下(也在上面)代碼最終在 startup.cs 中聲明為 null

                    var controller = context.Request.RouteValues["Abstract"];

希望了解我做錯了什么。

MapGet("/api/abstract/{showYear}/{showCode}", ...);

這段代碼不是負責將該路由與我的 Controllers 文件夾中名為 AbstractController.cs 的 controller 映射嗎? 沒有遇到任何斷點。

編輯:通讀這篇比較了.Net Core 2、2.2、MVC 項目與 3.0 的 Startup.cs 差異的文章,閱讀所有這些內容我感覺很好,我會找到問題所在。 https://andrewlock.net/comparing-startup-between-the-asp-net-core-3-templates/編輯..沒關系沒找到解決辦法。

編輯 2

在startup.cs中完全注釋掉這段麻煩的代碼:

endpoints.MapGet("/api/abstract/{showYear}/{showCode}", async context =>
            {
                var controller = context.Request.RouteValues["Abstract"];

                var showYear = context.Request.RouteValues["showYear"];
                var showCode = context.Request.RouteValues["showCode"]; // just an example.
                //await context.Response.WriteAsync($"Hello {showYear} is your year, {showCode} for the code!");
                //GetAbstractByYearAndSHCode();
            });
            
            endpoints.MapGet("/api/abstract/{showYear}", async context =>
            {
                var name = context.Request.RouteValues["name"];
                await context.Response.WriteAsync($"Hello {name}!");
            });

解決了我無法到達 controller 的問題。

在此處輸入圖像描述

https://localhost:44363/api/abstract/2019命中,id值為2019。太好了。 https://i.imgur.com/rmHyDPg.png output 看起來很棒。

我仍然無法使用 > 1 參數。 如何簡單地使用 Year 和 ShowCode 參數? 語法是什么? https://localhost:44363/api/abstract/2019

只需將參數添加到您的屬性


[HttpGet("{ShowYear}/{ShowCode}")]

第一種方法已經使用了“api/abstract”路由。 您不能再次將其用於其他操作。 如圖所示創建一個新的。

[HttpGet("{GetAbstractByYearAndSHCode}",Name = "GetAbstractByYearAndSHCode")]
public Abstract Get(int ShowYear, int ShowCode) // GetAbstractByYearAndSHCode
{
    string x = "";
    return new Abstract();
}

並調用 url,如圖所示:

https://localhost/api/abstract/GetAbstractByYearAndSHCode?ShowYear=1&ShowCode=5

暫無
暫無

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

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