【发布时间】:2013-01-09 18:35:08
【问题描述】:
我目前正在学习asp.net mvc,刚开始,我决定从web表单转向mvc。
我正在从 codeplex the mvc 音乐商店学习本教程,有这行代码我不明白它的使用方式和使用原因。
这是代码行:
if(!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
context.Session[CartSessionKey] = context.User.Identity.Name;
}
我想知道 context.User.Identity.Name 做了什么,因为我尝试删除它包含的 if 块并且应用程序仍然可以工作。
这是该函数的完整代码:
public string GetCartId(HttpContextBase context)
{
if (context.Session[CartSessionKey] == null)
{
if(!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
context.Session[CartSessionKey] = context.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
context.Session[CartSessionKey] = tempCartId.ToString();
}
}
return context.Session[CartSessionKey].ToString();
}
【问题讨论】:
标签: c# asp.net asp.net-mvc-3 visual-studio