-4

我在 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?我要求有人解释它在这个例子中的用途。

4

3 回答 3

2

搜索“C# GUID”会让您得到一个简单的答案:MSDN

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

请在发布问题之前检查文档。

于 2012-04-26T22:07:38.583 回答
1

Have a look at part 8 of that tutorial, under the heading "Managing the Shopping Cart business logic" - this explains the purpose of that code.

Basically, the GUID is used to uniquely identify a user, without forcing them to have logged in, so that the application can keep track of items that they have put into the shopping cart.

于 2012-04-27T04:33:53.743 回答
0

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

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

于 2012-04-26T22:07:07.947 回答