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

古いコードはすべて変数が宣言されていないと言っています。ページを実行すると、 '} expected'や 'identifier expected'や 'expected expected'のような行と行のエラーが表示されます。エラー – Trent

+0

質問を編集して、最小限の完全な例(すべてのエラーメッセージをそのまま含む)を表示してください。私は質問が今答えられ、閉鎖されていることを知っているが、それを改善するにはそれほど遅くはない。 –

答えて

1

彼らは@functionsセクションに行くと、あなたは、アクセシビリティ修飾子を必要としない、なぜ私はわからない:

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

これは完璧です、おかげで束私は特定のセクションが必要なのか分かりませんでした – Trent

+0

私はできるだけタイマーを待っていますこれを受け入れる – Trent

関連する問題