NET 6 Core 使用页面发布到自定义方法

NET 6 Core using Pages posting to a custom method

将 NET 6 核心应用程序与页数结合使用。

开箱即用的项目具有以下“代码隐藏”

public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;

    public IndexModel(ILogger<IndexModel> logger)
    {
        _logger = logger;
    }

    public void OnGet()
    {

    }

    // This is this the new code 
    public void MyCustomMethod()
    {
        var debug = "Stop Here debug break point";
    }
}

前端有以下内容:

@page
@model IndexModel
@{
   ViewData["Title"] = "Home page";
}

<div class="text-center">
   <form action="MyCustomMethod" method="get">
       <input type="submit" />
   </form>
</div>

无法 GETPOSTNET 6 中的自定义方法 (MyCustomMethod)使用 PAGES.

的核心

这可能是一个愚蠢的问题,因为我在过去 2 小时内尝试 google,但找不到与我正在做的事情相关的任何内容。请帮忙。

尝试使用简单的形式获取或post“代码隐藏”。当页面加载时,我确实看到调试进入 OnGet() 方法,它应该这样做,但是单击表单中的按钮它永远不会进入 MyCustomMethod()。

谢谢。

是否对代码进行了以下更改:

    [HttpPost] // Second run added this because it was still not working
    public void OnPostMyCustomMethod()
    {
        var debug = "Stop Here debug break point";
    }

Front page
    <form action="MyCustomMethod" method="post">
    <input type="submit" />
</form>

仍然无法访问:

Debug Run

Razor Pages 包含一个称为“命名处理程序方法”的功能。此功能使您能够指定可以为单个动词执行的多个方法。

如果您在后端代码中设置了 OnPostMyCustomMethod 或 MyCustomMethod,您应该使用这个 url "https://localhost:7086/?handler=MyCustomMethod" 来访问它。

更多细节,您可以参考下面的例子:

  <form asp-page-handler="MyCustomMethod"   method="post">
       <input type="submit" />
   </form>