Domanda

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;
    }
}
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top