Last post Nov 24, 2016 12:06 PM by PatriceSc
Nov 22, 2016 11:29 PM|omoOba14|LINK
I have method in a controller to run but every time the view reload the method run again. I tired running it from the application_start I got the same result every time the view reload, it the method runs again. How can I do it to just run once?.
Nov 23, 2016 11:55 AM|Cathy Zou|LINK
From your description, I suggest you could do as the code below:
public static bool flag = true;
public ActionResult Index3()
//call the method
ViewBag.Message = "Fist time load";
ViewBag.Message = "Second time load";
flag = false;
Nov 23, 2016 12:02 PM|omoOba14|LINK
Thank you very much, I want to ask does it state persist even when the view associate with ActionResult reload.
Nov 23, 2016 10:38 PM|PaulTheSmith|LINK
Do you mean to run the code once in the lifetime of the universe?
Or once everytime the web server starts?
Or once everytime the web site starts?
Or once everytime a new user comes to the site?
Or something else?
Nov 24, 2016 12:06 PM|PatriceSc|LINK
No the web is stateless and won't persist anything unless asking for this explicitely (if coming from Web Forms, it uses an hidden field to automatically persist few things for you).
Also doing that from Application_Start doesn't seems to make sense. I'm really not sure what you are trying to do. A possible option might be to "cache" this page but for now I would suggest to explain what is your final goal.