如何从一个变量届时AppState参考的数据? [英] How can I reference data from an AppState variable?

查看:291
本文介绍了如何从一个变量届时AppState参考的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是,我无法找到一个方法来一个if条件内测试存储在届时AppState [变量名(或App.variableName或HttpContext.Current.Application [变量名]等)的任何东西。 (它永远只能看到它作为一个对象,即使我可以用剃刀为字符串变量绘制在页面上我认为这是)



所以,我可以' ŧ比较他们说一个实际的字符串值。



我试过的ToString(),其他无数的尝试中,都无济于事。



我的问题是:我如何才能实现完整的功能,在WebMatrix的网页都用C#届时AppState变量


解决方案

这里的问题在于,铸造是需要的,没有铸造和届时AppState变量之间的空间。在我张贴了这个问题的时候,我还是那么新的(当然,仍然感到很)到C#服务器端编程。什么工作的一个例子是:



  IF((串)届时AppState [MYVARIABLE] ==someString)
{
//做一些东西
}

此外,无论是多人们喜欢的全局变量与否来看,届时AppState变量,事实上,考虑全局变量。这显然​​是在第一行迈克Brind的Mikesdotnetting文章ASP.NET网页之间Tranferring数据下的应用程序变量声明:

 应用程序的变量也称为全局变量。 - (Mikesdotnetting)



另外,如果你(你是谁)没有读过这篇文章,是无论是新WebMatrix的或想看到所有的选项,在WebMatrix的页面之间传输数据时,请不要自己一个巨大的青睐和阅读易于阅读,写得很好,并富有教育意义的文章在这里找到:



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


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#?

解决方案

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

这篇关于如何从一个变量届时AppState参考的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆