<sessionState mode="Off"/> <anonymousIdentification enabled="true"/>
<authentication mode="form"> <forms name="PetShopAuth" loginUrl="SignIn.aspx" protection="None" timeout="60"/> </authentication>
<profile automaticSaveEnabled="false" defaultProvider="ShoppingCartsProvider"> <providers> <add name="ShoppingCartsProvider" connectionStringName="SQLProfileConnString" type="PetShopProfileProvider"/> </providers> <properties> <add name="ShoppingCart" type="Cart" allowAnonymous="true" provider="ShoppingCartsProvider"/> </properties> </profile>
Cart购物车类,主要是对Profile的操作
/// <summary> ///Cart 的摘要说明 /// </summary> [Serializable] public class Cart { // Dictionary: key/value private Dictionary<string, CartItemInfo> cartItems = new Dictionary<string, CartItemInfo>(); /// <summary> /// 添加信息 /// </summary> /// <param name="itemId"></param> public void Add(string itemId) { } /// <summary> /// Add an item to the cart. /// When ItemId to be added has already existed, this method will update the quantity instead. /// </summary> /// <param name="item">Item to add</param> public void Add(CartItemInfo item) { CartItemInfo cartItem; if (!cartItems.TryGetValue(item.ItemId, out cartItem)) cartItems.Add(item.ItemId, item); else cartItem.Quantity += item.Quantity; } /// <summary> /// 删除信息 /// </summary> /// <param name="itemId"></param> public void Remove(string itemId) { } /// <summary> /// 信息列队 /// </summary> public ICollection<CartItemInfo> CartItems { get { return cartItems.Values; } } /// <summary> /// 清空信息 /// </summary> public void Clear() { } }
PetShopProfileProvider类,数据更新的操作
/// <summary> ///ProfileProvider 的摘要说明 /// </summary> public sealed class PetShopProfileProvider : ProfileProvider { private static readonly IPetShopProfileProvider dal = DataAccess.CreatePetShopProfileProvider(); public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate) { throw new NotImplementedException(); } public override int DeleteProfiles(string[] usernames) { throw new NotImplementedException(); } public override int DeleteProfiles(ProfileInfoCollection profiles) { throw new NotImplementedException(); } public override ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords) { throw new NotImplementedException(); } public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) { throw new NotImplementedException(); } public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords) { throw new NotImplementedException(); } public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords) { throw new NotImplementedException(); } public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate) { throw new NotImplementedException(); } private const string ERR_INVALID_PARAMETER = "Invalid Profile parameter:"; private const string PROFILE_SHOPPINGCART = "ShoppingCart"; private static string applicationName = ".NET Pet Shop 4.0"; public override string ApplicationName { get { return applicationName; } set { applicationName = value; } } public override void Initialize(string name, NameValueCollection config) { if (config == null) throw new ArgumentNullException("config"); if (string.IsNullOrEmpty(config["description"])) { config.Remove("description"); config.Add("description", "Pet Shop Custom Profile Provider"); } if (string.IsNullOrEmpty(name)) name = "PetShopProfileProvider"; if (config["applicationName"] != null && !string.IsNullOrEmpty(config["applicationName"].Trim())) applicationName = config["applicationName"]; base.Initialize(name, config); } public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection) { string username = (string)context["UserName"]; bool isAuthenticated = (bool)context["IsAuthenticated"]; SettingsPropertyValueCollection svc = new SettingsPropertyValueCollection(); foreach (SettingsProperty prop in collection) { SettingsPropertyValue pv = new SettingsPropertyValue(prop); switch (pv.Property.Name) { case PROFILE_SHOPPINGCART: Cart cart = new Cart(); CartItemInfo ci = new CartItemInfo("ssd", "gxac", 1, 1, "ssd", "q", "s1"); cart.Add(ci); // pv.PropertyValue = cart; pv.PropertyValue = GetCartItems(username, true); break; default: throw new ApplicationException(ERR_INVALID_PARAMETER + " name."); } svc.Add(pv); } return svc; } // Retrieve cart private static Cart GetCartItems(string username, bool isShoppingCart) { Cart cart = new Cart(); foreach (CartItemInfo cartItem in dal.GetCartItems(username, applicationName, isShoppingCart)) { cart.Add(cartItem); } return cart; } public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) { throw new NotImplementedException(); } }
购物车model
[Serializable] public class CartItemInfo { // Internal member variables private int quantity = 1; private string itemId; private string name; private string type; private decimal price; private string categoryId; private string productId; /// <summary> /// Default constructor /// </summary> public CartItemInfo() { } /// <summary> /// Constructor with specified initial values /// </summary> /// <param name="itemId">Id of item to add to cart</param></param> /// <param name="name">Name of item</param> /// <param name="qty">Quantity to purchase</param> /// <param name="price">Price of item</param> /// <param name="type">Item type</param> /// <param name="categoryId">Parent category id</param> /// <param name="productId">Parent product id</param> public CartItemInfo(string itemId, string name, int qty, decimal price, string type, string categoryId, string productId) { this.itemId = itemId; this.name = name; this.quantity = qty; this.price = price; this.type = type; this.categoryId = categoryId; this.productId = productId; } // Properties public int Quantity { get { return quantity; } set { quantity = value; } } public decimal Subtotal { get { return (decimal)(this.quantity * this.price); } } public string ItemId { get { return itemId; } } public string Name { get { return name; } } public string Type { get { return type; } } public decimal Price { get { return price; } } public string CategoryId { get { return categoryId; } } public string ProductId { get { return productId; } } }
用法
protected void Button1_Click(object sender, EventArgs e) { ICollection<CartItemInfo> info = Profile.ShoppingCart.CartItems; GridView1.DataSource = info; GridView1.DataBind(); string username = (string)Profile.Context["UserName"]; Response.Write(username); }