简体   繁体   English

当前上下文中不存在名称“会话”

[英]The name 'Session' does not exist in the current context

I'm having trouble figuring out how to get a session to work and I feel like I'm probably missing something really obvious.我在弄清楚如何让会话工作时遇到了麻烦,我觉得我可能错过了一些非常明显的东西。 I have a master page in an ASP project and in it I have this code for the logged-in event of a log-in object on the master page:我在 ASP 项目中有一个母版页,在其中我有这个母版页上登录对象的登录事件的代码:

protected void Login1_LoggedIn(object sender, EventArgs e) 
    {
        Session.Add("Username", Login1.UserName);
    }

So the session should contain the username used in the Log-in object to log in所以会话应该包含登录对象中使用的用户名来登录

In the master page's contents I'm just trying a simple reference by making it show the username used to log in:在母版页的内容中,我只是通过显示用于登录的用户名来尝试一个简单的参考:

Welcome <%Response.Write(Session("Username"))%>

But "session" is underlined in red, saying "The name 'Session' does not exist in the current context".但是“会话”以红色下划线标出,表示“当前上下文中不存在名称'会话'”。 I've been trying to figure out what I'm missing but so far what I've tried hasn't done anything.我一直在试图弄清楚我错过了什么,但到目前为止我尝试过的还没有做任何事情。

Any Advice would be very appreciated.任何建议将不胜感激。

I think it's maybe a problem of a namespace我认为这可能是命名空间的问题

Do you can try with :你可以试试:

<%Response.Write(HttpContext.Current.Session("Username"))%>
Session["UserAuthentication"] = username;

使用 [] 而不是 ()。

像这样使用: session["authentication"]

添加 using 语句。

using System.Web;

试试这段代码

HttpContext.Current.Session["abc"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM