【发布时间】:2021-09-27 20:03:56
【问题描述】:
我正在使用 ASP.NET CORE 5.0 版,并且正在为我的 Web UI 使用剃须刀页面。我在使用 HTML 中具有以下结构的简单 OnPost 方法时遇到问题:
@page
@model EmptyOne.Web.Pages.SaysHelloModel
@{
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Search</title>
</head>
<body>
<p>Enter the name you wanna say hello two ;)</p>
<form method="post">
Search term:
<input name="nameToHello" />
<input type="submit" />
</form>
@* some logic to say hello if model is not null... *@
</body>
</html>
这是我的页面模型类:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace EmptyOne.Web.Pages
{
public class SaysHelloModel : PageModel
{
public string guysName { get; set; }
public void OnGet()
{
}
public IActionResult OnPost(string nameToHello)
{
guysName = nameToHello;
return Page();
}
}
}
当我尝试点击它时,它会返回一个 HTTP 400 状态核心和一个空白页面,但由于某种原因,当我添加这个助手时:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
效果很好!
最终的工作视图(我没有更改页面模型类中的任何内容):
@page
@model EmptyOne.Web.Pages.SaysHelloModel
@{
}
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Search</title>
</head>
<body>
<p>Enter the name you wanna say hello two ;)</p>
<form method="post">
Search term:
<input name="nameToHello" />
<input type="submit" />
</form>
@* some logic to say hello if model is not null... *@
</body>
</html>
那么任何人都可以向我解释为什么会这样吗?我查找了一个 ASP.NET Core 5.0 版代码示例并看到了这个帮助程序。我是否以任何方式使用这个助手?它与向我的页面处理程序发送 HTTP 发布请求有什么关系?
【问题讨论】:
标签: httprequest razor-pages asp.net-core-5.0 asp.net-core-tag-helpers