【发布时间】:2023-01-15 00:23:31
【问题描述】:
我对 C#、.NET 或 MVC 模式的了解还不够,无法确切地知道要在此处包含哪些相关内容,但我正在通过一个非常简单的更改来解决问题。
我有一个带有搜索操作(方法?)的控制器,如下所示:
public string Search(int id)
{
return $"The id was {id}";
}
当我到达路线时,我得到了预期的回应,例如
$ curl https://localhost:7180/Players/Search/1
The id was 1
但是当我将变量名从 id 更改为其他任何名称时,行为会发生变化并且由于某种原因该值变为 0。
public string Search(int thing)
{
return $"The thing was {thing}";
}
$ curl https://localhost:7180/Players/Search/1
The thing was 0
我认为这可能与模型本身有关,因为模型代码至少具有 Id 属性
public class Player
{
public int Id { get; set; }
public string? Name { get; set; }
}
但将该变量重命名为 name(这看起来很相似)也无济于事。
那么我在这里缺少什么概念?为什么我不能将该变量重命名为我想要的任何名称?提前致谢!
(我不知道如何更好地传达代码的所有不同方面,所以这里是link to the line in question, inside the project)
【问题讨论】:
-
在你的控制器中,尝试:
[HttpGet("{thing}")] public string Search(int thing) { return $"The thing was {thing}"; } -
啊,是的,在
Program.cs我有声明app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");。
标签: c# asp.net-mvc