类文件里面为什么不能写这样的代码

snria 2011-06-01 11:23:26
这是一个User类

public class User
{
private int _id;
private string _username;
public int Id
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
/// <summary>
/// 返回用户的链接
/// </summary>
/// <param name="hasAt">是否包含@符号</param>
/// <returns></returns>
public string PutUserHref(bool hasAt)
{
if (hasAt)
{
return string.Format("<a href='/t/?uid={0}' class='userLink' target='_top'>@{1}</a>", this.Id, this.Name);
}
else
{
return string.Format("<a href='/t/?uid={0}' class='userLink' target=\"_top\">{1}</a>", this.Id, this.Name);
}
}
/// <summary>
/// 判断是否登录
/// </summary>
/// <returns></returns>
public static bool IsLogin()
{
if (HttpContext.Current.Session["User"] == null || HttpContext.Current.Session["Company"] == null)
{
if (HttpContext.Current.Request.Cookies["Email"] != null && HttpContext.Current.Request.Cookies["Password"] != null)
{
string email = HttpContext.Current.Request.Cookies["Email"].Value;
string pass = HttpContext.Current.Request.Cookies["Password"].Value;
return LoginByEmail(email, Common.DEncrypt.DEncrypt.Decrypt(pass));
}

return false;
}
else
{

return true;
}
}
}

为什么User类里面不能有PutUserHref的属性。我这个属性很多地方的都用到的啊。绑定数据的时候方便的很啊。难道我包了一下还有错?

还有就是IsLogin方法。为什么不能再类文件用Session。为什么?




...全文
97 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
种草德鲁伊 2011-06-01
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 snria 的回复:]

项目经理说类文件里面不能出现任何处理html代码的代码以及session....

[/Quote]

那样说也绝对是错的,保持职责单一,不是不能用。
snria 2011-06-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wxr0323 的回复:]

引用 7 楼 snria 的回复:

引用 2 楼 wxr0323 的回复:

需要使用什么 就要添加相关using.


我问的是为什么不能写,不是问的不能用... 我知道添加引用后就可以用了

添加引用就可以写了.
[/Quote]
代码早就写好了.也能运行正常.但项目经理看到了说 不允许在User写这2种方法.说这样写,思想上就是有错误的.说类文件里面不能出现任何处理html代码的代码以及session....
种草德鲁伊 2011-06-01
  • 打赏
  • 举报
回复


public SessionState Session
{
get { return HttpContext.Current.Session; }
}

种草德鲁伊 2011-06-01
  • 打赏
  • 举报
回复
你平常在 aspx或codebehind的代码文件里可以用 Session 这个东西。因为他是 Page 或 Control 的一个属性。
跟asp不一样,它不是全局变量。
子夜__ 2011-06-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 snria 的回复:]

引用 2 楼 wxr0323 的回复:

需要使用什么 就要添加相关using.


我问的是为什么不能写,不是问的不能用... 我知道添加引用后就可以用了
[/Quote]
添加引用就可以写了.
snria 2011-06-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wxr0323 的回复:]

需要使用什么 就要添加相关using.
[/Quote]

我问的是为什么不能写,不是问的不能用... 我知道添加引用后就可以用了
snria 2011-06-01
  • 打赏
  • 举报
回复
....拜托 我是问的为什么不能写 不是不能用
种草德鲁伊 2011-06-01
  • 打赏
  • 举报
回复
因为.net没有全局变量。
porschev 2011-06-01
  • 打赏
  • 举报
回复

用session

引用using System.Web.SessionState;命名空间

继续IRequiresSessionState接口
  • 打赏
  • 举报
回复
先using System.Web;

Session是WEB空间下的对象
子夜__ 2011-06-01
  • 打赏
  • 举报
回复
需要使用什么 就要添加相关using.
孟子E章 2011-06-01
  • 打赏
  • 举报
回复
先using System.Web;

or

System.Web.HttpContext.Current.Request.Cookies

。。。。

62,248

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧