“Session”在当前上下文中不存在["ShoppingCart"]==null)TheerroriscS0103:Thename'Session'doesnotexistinthecurrentcontext什么问题?使用系统数据;使用系统配置;使用System.Linq;使用System.Web;使用System.Web.Security;使用System.Web.UI;使用System.Web.UI.HtmlControls;使用System.Web.UI.WebControls.WebParts;使用System.Xml.Linq;使用System.Collections.Generic;使用System.Web.SessionState;//////ShoppingCart的摘要描述///publicclassShoppingCart{Listlist;publicShoppingCart(){if(Session["ShoppingCart"]==null)list=newList();elselist=(List)Session["ShoppingCart"];}}问题是你的类不从页面继承。将publicclassShoppingCart更改为publicclassShoppingCart:Page,它将使用if(HttpContext.Current==null||HttpContext.Current.Session==null||HttpContext.Current.Session["ShoppingCart"]==null)代替if(Session["ShoppingCart"]==null)你需要通过继承将你的类从Page转换为Page,或者传入Session,或者使用HttpContext.Current.Session。萨拉姆。在我的例子中,只有try-catch块解决了这个问题,就像这样:错误。try{//必须包含错误处理,因为这适用于更广泛的页面//让系统回退到不变if(Session["_culture"]!=null){System.Threading.Thread.CurrentThread.CurrentCulture=newSystem.Globalization.CultureInfo(Session["_culture"].ToString());//确保向其提供特定文化并避免异常会更安全}}catch(Exceptionex){}}从损坏的页面创建一个新页面并将所有内容粘贴到其中是行不通的。起作用的是关闭VisualStudio并重新打开它。之后坏掉的页面就可以了如果想直接使用session,只需要在system.web.mvc中添加如下命名空间以上是C#学习教程:“session”在当前上下文中不存在。很有用,需要多了解C#学习教程。希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
