2017-02-03 42 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