How can I reference data from an AppState variable?

只谈情不闲聊 提交于 2019-12-03 00:55:18

问题


The problem is that I can't find a way to test anything stored in AppState["variableName"] (or App.variableName or HttpContext.Current.Application["variableName"], etc.) inside an if condition. (It only ever sees it as an object, even though I can plot it on the page with Razor as the string variable I thought it was)

So, I can't compare them to say an actual string value.

I've tried ToString(), among countless other attempts, to no avail.

My question is: How can I achieve full functionality with the AppState variable in WebMatrix Web-Pages with C#?


回答1:


The problem here is that casting is needed, without a space between the cast and the AppState variable. At the time that I posted this question, I was still so new (well, still am really) to C# server side programming. An example of what works is:

if ((string)AppState["myVariable"] == "someString")
{
    //do some stuff
}

Also, whether many people like the term "global variable" or not, the AppState variable is, in fact, considered a global variable. This is clearly stated in Mike Brind's Mikesdotnetting article "Tranferring Data Between ASP.NET Web Pages" in the very first line under Application Variables:

"Application variables are also known as global variables." --(Mikesdotnetting)

Also, if you (whoever you are) have not read this article and are either new to WebMatrix or want to see all of the options for transferring data between pages in WebMatrix, please do yourself a tremendous favor and read this easy-to-read, well-written, and highly educational article found here:

http://www.mikesdotnetting.com/Article/192/Transferring-Data-Between-ASP.NET-Web-Pages



来源:https://stackoverflow.com/questions/12537228/how-can-i-reference-data-from-an-appstate-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!