我对.NET Core(尤其是ASP.NET)还比较陌生,想知道在视图中创建C#函数时是否有明显的错误。
我的视图如下(Collections.cshtml):
<h2>Collections</h2>
<div id="content">
@{
// define variables
List<string> collections;
int counter;
// build list
collections = new List<string>();
collections.Add("Nothing 01");
collections.Add("Nothing 02");
// display list
counter = 0;
while(counter < collections.Count)
{
<p>@collections[counter]</p>
counter = counter + 1;
}
}
</div>
这一切都很正常,也实现了我想要的功能,但是如果我尝试将其组织成函数或甚至添加基本函数,它会像下面一样崩溃。
@{
// function for no reason
public void testFunc()
{
string nothing;
nothing = null;
return;
}
}
<h2>Collections</h2>
<div id="content">
@{
// define variables
List<string> collections;
int counter;
// build list
collections = new List<string>();
collections.Add("Nothing 01");
collections.Add("Nothing 02");
// display list
counter = 0;
while(counter < collections.Count)
{
<p>@collections[counter]</p>
counter = counter + 1;
}
}
</div>
只是添加那个功能会导致出错,我不确定原因。