Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6575795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:25:53+00:00 2026-05-25T15:25:53+00:00

First some background, we are creating a new eGov application. Eventually, a citizen can

  • 0

First some background, we are creating a new “eGov” application. Eventually, a citizen can request permits and pay for licenses along with pay their utility bills and parking tickets online. Our vision has a shopping cart so a person can pay for multiple items in one transaction. To keep things organized better, we are going to break each section into a different project. This also allows me to work on one project while the other developer works another. The person making a payment could be a registered user or could remain unregistered. We feel a person from out of our jurisdiction probably doesn’t want to register just to pay their parking ticket or pay for a one-time business license.

This project will be on Windows Server 2008 and IIS7 and using ASP.NET MVC 3. We will probably use a single domain (maybe egov.domain.gov) and in multiple sub directories (/cart, /permit, /billing, etc) though that is not 100% decided yet.

Now the problem. How do we track a shopping cart across multiple projects? There was talk of using a cookie that expires at a certain point or using a state machine. We are uncertain if using a session id would work. If we use a state machine, I have never used that and only understand it in concept (it works across multiple machines and SO uses it).

One other note, we are going to be building this on a VMWare server, so the possibility of having this run across multiple servers is a possibility in the future.

What would you use and why?

Update: It appears like many seem to recommend storing the cart in HttpContext. Is this the same across multiple applications?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:25:54+00:00Added an answer on May 25, 2026 at 3:25 pm

    First you need to setup your SQL Server to accept session state connections.

    • Article 1
    • Article 2

    Then add the following to your Web.config file:

    <sessionState mode="SQLServer" sqlConnectionString="Server=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ASPState;Application Name=name" timeout="20" allowCustomSqlDatabase="true" />` within `<system.web>
    

    I then created a class library that has two classes: Cart and CartItem.

    CartItem defined to hold each individual shopping cart item

    [Serializable]
    public class CartItem
    {
        [Key]
        public int RecordId { set; get; }
        public string ItemNumber { set; get; }
        public string Description { set; get; }
        public DateTime DateTimeCreated { set; get; }
        public decimal Cost { get; set; }
    }
    

    Cart works with your shopping cart

    public class Cart
    {
        HttpContextBase httpContextBase = null;
        public const string CartSessionKey = "shoppingCart";
    
        /// <summary>
        /// Initializes a new instance of the <see cref="ShoppingCart"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public Cart(HttpContextBase context)
        {
            httpContextBase = context;
        }
    
        /// <summary>
        /// Gets the cart items.
        /// </summary>
        /// <returns></returns>
        public List<CartItem> GetCartItems()
        {
            return (List<CartItem>)httpContextBase.Session[CartSessionKey];
        }
    
        /// <summary>
        /// Adds to cart.
        /// </summary>
        /// <param name="cartItem">The cart item.</param>
        public void AddToCart(CartItem cartItem)
        {
            var shoppingCart = GetCartItems();
    
            if (shoppingCart == null)
            {
                shoppingCart = new List<CartItem>();
            }
    
            cartItem.RecordId = shoppingCart.Count + 1;
            cartItem.DateTimeCreated = DateTime.Now;
            shoppingCart.Add(cartItem);
    
            httpContextBase.Session[CartSessionKey] = shoppingCart;
        }
    
        /// <summary>
        /// Removes from cart.
        /// </summary>
        /// <param name="id">The id.</param>
        public void RemoveFromCart(int id)
        {
            var shoppingCart = GetCartItems();
            var cartItem = shoppingCart.Single(cart => cart.RecordId == id);
            shoppingCart.Remove(cartItem);
            httpContextBase.Session[CartSessionKey] = shoppingCart;
        }
    
        /// <summary>
        /// Empties the cart.
        /// </summary>
        public void EmptyCart()
        {
            httpContextBase.Session[CartSessionKey] = null;
        }
    
        /// <summary>
        /// Gets the count.
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return GetCartItems().Count;
        }
    
        /// <summary>
        /// Gets the total.
        /// </summary>
        /// <returns></returns>
        public decimal GetTotal()
        {
            return GetCartItems().Sum(items => items.Cost);
        }
    }
    

    To test this, first in my shopping cart project in my home controller I did the following:

        public ActionResult Index()
        {
            var shoppingCart = new Cart(this.HttpContext);
            var cartItem = new CartItem
            {
                Description = "Item 1",
                ItemNumber = "123"
                Cost = 20,
                DateTimeCreated = DateTime.Now
            };
    
            shoppingCart.AddToCart(cartItem);
    
            cartItem = new CartItem
            {
                Description = "Item 2",
                ItemNumber = "234"
                Cost = 15,
                DateTimeCreated = DateTime.Now
            };
    
            shoppingCart.AddToCart(cartItem);
    
            var viewModel = new ShoppingCartViewModel
            {
                CartItems = shoppingCart.GetCartItems(),
                CartTotal = shoppingCart.GetTotal()
            };
    
            return View(viewModel);
        }
    

    In my second project’s home controller, I added the following:

        public ActionResult Index()
        {
            var shoppingCart = new Cart(this.HttpContext);
            var cartItem = new CartItem
            {
                Description = "Item 3",
                ItemNumber = "345"
                Cost = 55,
                DateTimeCreated = DateTime.Now
            };
    
            shoppingCart.AddToCart(cartItem);
    
            return View();
        }
    

    This seemed to work for me great.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First some background: VB.NET 2005 Application that accesses a MS-SQL back-end, using multiple Web
First, some background: I'm new to ASP.NET MVC 2 and NHibernate. I'm starting my
As usual, some background information first: Database A (Access database) - Holds a table
Hopefully some Custom Control Designers/Builders can help I'm attempting to build my first custom
First i need some background color on the text only. Like the headers in
Wanting to implement authentication by client certificates I am experiencing some issues. First some
I'm reading input in a C++ program. First some integers, then a string. When
When I need some complex algorithm I first check if there's anything relevant already
I remember first learning about vectors in the STL and after some time, I
This is my first post here and I wanted to get some input from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.