BAL(3层)层中的会话访问 [英] Session Access in BAL(3-tier) Layer

查看:113
本文介绍了BAL(3层)层中的会话访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我已经在3层创建了Web应用程序,并且我想在会话中存储登录用户信息并希望在BAL中访问。

Hello,

I Have Created Web Application in 3-Tier, and I Want to Store Login User Info in Session and want To Access in BAL.

推荐答案

你可以做几件事



最简单的方法是

添加一个将system.web引用到BAL,

然后使用以下语法

You can do couple of things

Simplest way is
Add a reference of system.web to BAL,
then use the following syntax
HttpContext.Current.Session["MySession"]





其次正确方式是



i)创建一个引用System.Web的类库。在这个项目中创建Class Say ClsStateManagement。

在类中添加一个属性





Secondly Proper way is

i)Create a class library which has a reference to System.Web. create Class Say ClsStateManagement in this project.
Add a property to the class

public string UserId
   {
       get
       {
           return (string)HttpContext.Current.Session["UserId"];
       }
       set
       {
           HttpContext.Current.Session["UserId"]=value;
       }
   }





ii)将此项目的引用添加到UI和BAL



现在在你的ASPX.CS(用户界面)



ii) Add reference of this project to both you UI and BAL

Now in you ASPX.CS (in UI)

ClsStateManagement c=new ClsStateManagement()
c.UserId="MyUserId";







BAL




in BAL

ClsStateManagement c=new ClsStateManagement()
string myUserId=c.UserId;





如果有帮助,请将此标记为corerct答案。



Mark this as corerct answer if it helped.


将文本框值存储到会话中。



To store the textbox value into session.

Session["userid"]=txtUserName.Text.Trim()







谢谢




Thanks


大家好,





我有了Fi nal解决方案,HttpContext.Current.Session返回null的唯一原因是因为我在web.config中设置了CookieLess = true,如果我想从Web项目外部访问HttpContext.Current.Session,则默认为false。



谢谢
Hi All,


I Got the Final Solution, The only reason why HttpContext.Current.Session returning null is because i am set CookieLess =true in web.config , which is default false if i want to access HttpContext.Current.Session from outside the web project.

Thanks


这篇关于BAL(3层)层中的会话访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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