名称"isPost"在当前上下文中不存在(与Razor ASP.NET)



我正在尝试按照微软文档的方式开始使用Razor。当我尝试实现以下代码时,我得到了注释中详细的错误:

Solution/Project/Pages/AddNumbers.cshtml:

@{
var total = 0;
var totalMessage = "";
if(IsPost) { // The name 'isPost' does not exist in the current context
// Retrieve the numbers that the user entered.
var num1 = Request["text1"]; // CS0103  The name 'Request' does not exist in the current context
var num2 = Request["text2"]; // CS0103  The name 'Request' does not exist in the current context
// Convert the entered strings into integers numbers and add.
total = num1.AsInt() + num2.AsInt();
totalMessage = "Total = " + total;
}
}

我想我已经忠实地按照说明去做了,但是我想不起来我哪里出错了。解决办法是什么?

IsPost不存在,如果您不创建它。您可以创建这样的属性,默认为false,并在OnPost处理程序中将其设置为true。

同样,你不能像在你的代码中那样引用Request,你也需要一个属性。

由于num1num2没有成功初始化,它们也不能成功转换为int

我认为问题在于我所遵循的文档是针对ASP的。. NET Web Pages (Razor) 3",但我试图在"ASP上运行代码。. NET Core Web App"

正如Lajos Arpad指出的,我需要创建自己的isPost属性。

最新更新