什么是ASPNET GUID?

时间:2012-04-26 22:05:09

标签: asp.net-mvc-3 guid

我在Music Store MVC3教程的ShoppingCart课程中看到过这段代码:http://www.asp.net/mvc/tutorials/mvc-music-store

// We're using HttpContextBase to allow access to cookies.
        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();
        }

为什么需要GUID?我要求有人解释在这个例子中它用于什么。

3 个答案:

答案 0 :(得分:2)

搜索“C#GUID”会让您轻松回答:MSDN

GUID 表示全局唯一标识符,它与ASP.NET MVC无关。

请在发布问题前查看文档。

答案 1 :(得分:1)

在“管理购物车业务逻辑”标题下查看该教程的part 8 - 这解释了该代码的用途。

基本上,GUID用于唯一标识用户,而不强制他们登录,以便应用程序可以跟踪他们放入购物车的商品。

答案 2 :(得分:0)

GUID是全局唯一标识符。请参阅以下维基百科条目:

http://en.wikipedia.org/wiki/Globally_unique_identifier