2017-02-03 39 views
3

我对.NET Core很感兴趣(整体而言,ASP.NET),我想知道在我的视图中创建C#函数时是否会做出明显错误的任何错误.NET Core Razor 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> 

刚刚加入该功能打破它,我不知道为什么

+3

“它打破了”不告诉我们任何关于你得到什么错误的信息。 –

+0

所有旧的代码开始说变量没有被声明,当我运行页面时,它显示了错误的行和行,像'}预期'和'预期标识符'和'预期类型'等至少50行错误 – Trent

+0

请编辑该问题以显示一个*最小但完整的*示例(其中包含所有错误消息逐字)。我知道这个问题现在已经得到解答并且已经结束,但现在改进它还为时不晚。 –

回答

1

他们走在@functions部分,你并不需要一个可访问性修饰符:

@functions { 
    // function for no reason 
    void testFunc() 
    { 
     string nothing; 
     nothing = null; 
     return; 
    } 
} 
+0

这是完美的,谢谢一堆我不知道我需要一个特定的部分 – Trent

+0

我只是在等待计时器,所以我可以接受这个 – Trent