2014-03-14 30 views
1

我不明白當我設置我的類變量時我做錯了什麼。例如,當我撥打setHelloWorld()時,返回helloworld is: Hello World。然後當我撥打getHelloWorld()helloworld is:時,返回。爲什麼helloworld在設置後表現爲空字符串?類變量MVC 2應用程序

public class HomeController : Controller { 
    string helloworld; 


    public string setHelloWorld(){ 
     helloworld = "Hello World"; 
     return "helloworld is: " + helloworld; 
    } 

    public string getHelloWorld() { 
      return "helloworld is: " + helloworld; 
    } 
} 

回答

2

控制器的實例是根據請求創建的,所以沒有保存狀態。這是網絡無狀態的一部分。

如果您需要持久保存多個請求,您可以考慮將它放入會話狀態或http緩存中。