문제

I don't understand what I am doing wrong when setting my Class variables. For instance, when I call setHelloWorld(), helloworld is: Hello World is returned. Then when I call getHelloWorld(), helloworld is:, is returned. Why is helloworld behaving as an empty string after being set?

public class HomeController : Controller {
    string helloworld;


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

    public string getHelloWorld() {
            return "helloworld is: " + helloworld;
    }
}
도움이 되었습니까?

해결책

The instance of the controller is created per request, so no state is kept. It's part of the stateless nature of the web.

You can consider putting it in session state or http cache if you need it persisted for multiple requests.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top