將 NET 6 核心應用程式與Pages一起使用。
開箱即用的專案具有以下“代碼背后”
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>
無法使用 PAGES GET或POST到NET 6 CORE中的自定義方法 (MyCustomMethod) 。
這可能是一個愚蠢的問題,我在過去 2 小時內試圖用谷歌搜索,但找不到與我正在做的事情相關的任何事情。請幫忙。
嘗試使用簡單的表單來獲取或發布到“代碼背后”。當頁面加載時,我確實看到除錯進入 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>
仍然無法訪問:
除錯運行
uj5u.com熱心網友回復:
Razor Pages 包含一個稱為“命名處理程式方法”的功能。此功能使您可以指定可以為單個動詞執行的多個方法。
如果您在后端代碼中設定了 OnPostMyCustomMethod 或 MyCustomMethod,則應使用此 url "https://localhost:7086/?handler=MyCustomMethod" 訪問它。
更多細節,你可以參考下面的例子:
<form asp-page-handler="MyCustomMethod" method="post">
<input type="submit" />
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/436727.html
上一篇:如何使用CURL呼叫我的DjangoRESTAPI端點?
下一篇:訪問LinkedList中的物件
