为什么不是';我的C#.Net Core Rest API路由查找我的方法?

为什么不是';我的C#.Net Core Rest API路由查找我的方法?,c#,asp.net-mvc,asp.net-core,controller,routes,C#,Asp.net Mvc,Asp.net Core,Controller,Routes,我正在开发一个API。 我有一个“AbstractController.cs”,我很难用两个参数调用GET [Route("api/[controller]")] [ApiController] public class AbstractController : ControllerBase { // GET: api/Abstract [HttpGet] public IEnumerable<string> Get() {

我正在开发一个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" };
    }
无论我做什么,我都不能让这个方法断点/进入执行状态! 我猜这是一个路由问题,但我已经尝试了调用端点的最合理的方法

现在我像任何自尊的学习程序员一样检查MS文档。

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

没有路线的概念。不提供订购保证 为了执行可扩展性,所有端点都在 一次

大口大口喝听起来很吓人

我还没有触及我的中间件

我看过的所有示例的Startup.cs中都没有我的“MapControllers();”方法。 这是因为它是.NETCore3.0/3.1的新版本 方法:“

好的,那么我还需要在这里手动指定路线吗? 我这样做了,而且效果不错

现在,当我转到startup.cs时,它会在startup.cs中设置断点https://localhost:44363/api/abstract/2019/287

等等,它对我的控制器代码没有任何作用!为什么?

下面(也在上面)的代码最终在startup.cs中声明为null

                    var controller = context.Request.RouteValues["Abstract"];
希望知道我做错了什么

MapGet(“/api/abstract/{showYear}/{showCode},…)

这段代码不负责将该路由映射到“我的控制器”文件夹中名为AbstractController.cs的控制器吗?没有命中任何断点

编辑: 阅读本文,比较Startup.cs与.Net Core 2、2.2、MVC项目与3.0之间的差异 读了所有这些,我有一种很好的感觉,我会发现问题所在。 编辑nevermind没有找到解决方案

编辑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}!");
            });
解决了我无法联系控制器的问题

https://localhost:44363/api/abstract/2019 点击,id值为2019。伟大的 输出看起来很棒

我仍然无法使用>1个参数。如何简单地使用年份和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


如果您现在在.NC3.1中创建一个新项目,默认的WeatherForecastController.cs值为:[Route(“[controller]”)有趣。我以为我只见过卷发。说到重新开始;这对你的新项目有效吗?重新开始是什么意思?我想一下。这是新生成的控制器。你做的新项目;它有用吗?按play,打开postman,添加url,点击send request-是否调用该操作?对实际上,它是如何在原始版本中工作的。是的。转到localhost:44363/api/abstract/2019/248619打印漂亮的json。更改属性如下:并导航到localhost:44363/api/abstract/GetAbstractByYearAndSHCode?ShowYear=2019&ShowCode=248619后,我不返回任何json。这需要一段时间,然后404s。我假设HttpGet属性中的“Name”属性(GetAbstractByYearAndSHCode)是REST中的web方法名称。根据snippet中的要求更新了答案。您可以取消路由属性。不再需要它了。get属性路由在代码段中不正确。但您不需要route属性。
[HttpGet("{GetAbstractByYearAndSHCode}",Name = "GetAbstractByYearAndSHCode")]
public Abstract Get(int ShowYear, int ShowCode) // GetAbstractByYearAndSHCode
{
    string x = "";
    return new Abstract();
}