【问题标题】:Unable to get the id from OnGet method无法从 OnGet 方法获取 id
【发布时间】:2020-08-04 10:59:53
【问题描述】:

我有这个网址 https://localhost:44314/Identity/Role/Edit?id=408112a5-4fc9-4660-b308-b68d84f035b6

问题是当我为这两种方法设置断点时,它确实停在EditModel 而不是OnGet。这是一个 Razor 页面(不是 MVC)。

没有错误。它只是加载剃刀页面,但没有达到 onget 方法。

如果我将 URL 更改为 https://localhost:44314/Identity/Role/Edit,它可以到达OnGet

如果我将 URL 更改为 https://localhost:44314/Identity/Role/Edit/408112a5-4fc9-4660-b308-b68d84f035b6,则错误变为This localhost page can’t be found

我相信它与路由有关,因为常规路由是controller/action,而在这里,因为我有area,它变成identity/controller/action

但是,我仍然找不到解决方案。有人可以帮忙吗?

public class EditModel : PageModel
{
    public EditModel()
    {
    }

    public void OnGet(string id)
    {            
    }
}

【问题讨论】:

  • 顺便说一句,你得到什么回应?
  • 这能回答你的问题吗? Why do my Razor Page methods not get called?
  • https://localhost:44314/Identity/Role/Edit 中没有说要在这里使用此方法,请添加更多详细信息
  • .cshtml 页面顶部是否包含@page "{id:string}"?
  • 史蒂夫我从我有@page“{id:int}”的页面复制了它。忘记字符串是默认值,不需要。很高兴你解决了

标签: c# asp.net-core razor-pages


【解决方案1】:

在 Edit.cshtml 中添加 @page "{id}" 后问题解决。更多解释here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2020-10-01
    • 2021-05-15
    • 1970-01-01
    相关资源
    最近更新 更多