質問

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