【问题标题】:Model Binding from Route Parameter on Posted Form for Razor Page模型绑定来自 Razor 页面发布表单上的路由参数
【发布时间】:2021-09-17 11:13:25
【问题描述】:

我有以下 Razor 页面设置:

[BindProperties]
public class MyFormPageModel : PageModel
{
    public int Id { get; set; }

    public void OnGet(){}

    public IActionResult OnPost() {
       return RedirectToPage("Index", new {Id});
    }
}

还有:

@page "{Id:int?}"

<form method="post">
   <button type=submit>Submit</button>
</form>

如果我访问/MyForm/1,表单+按钮会加载。

点击按钮成功发帖到/MyForm/1

但这只会从后端产生 400 错误。

后端的输出日志中也没有显示错误。

【问题讨论】:

  • 你有av索引页吗?我在这里看不到任何东西

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


【解决方案1】:

找到我的问题。

在这种情况下没有生成防伪令牌,因为表单是动态生成的。

如果你从一开始就在 Razor .cshtml 中有一个&lt;form&gt;,Razor 会自动生成防伪令牌,但一旦你的表单是动态生成的,它就不起作用。

此页面为我的问题提供了一些出色的解决方案: https://exceptionnotfound.net/using-anti-forgery-tokens-in-asp-net-core-razor-pages/

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 2018-10-18
    • 2019-02-03
    • 2019-08-02
    • 2020-07-02
    • 1970-01-01
    • 2021-04-10
    • 2018-07-07
    • 2019-12-15
    相关资源
    最近更新 更多