Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何修复类型或命名空间';App#U代码';命名空间中不存在';WebApplication1';_C#_Asp.net - Fatal编程技术网

C# 如何修复类型或命名空间';App#U代码';命名空间中不存在';WebApplication1';

C# 如何修复类型或命名空间';App#U代码';命名空间中不存在';WebApplication1';,c#,asp.net,C#,Asp.net,我想创建一个购物车,但我得到了这个错误。如您所见,我在WebApplication1.App_代码命名空间中有ShoppingCart类,但当我尝试在Default.aspx.cs中使用WebApplication1.App_代码时,它不起作用 namespace WebApplication1.App_Code { public class ShoppingCart { public List<CartItem> Items { get; priva

我想创建一个购物车,但我得到了这个错误。如您所见,我在WebApplication1.App_代码命名空间中有ShoppingCart类,但当我尝试在Default.aspx.cs中使用WebApplication1.App_代码时,它不起作用

namespace WebApplication1.App_Code
{
    public class ShoppingCart
    {
        public List<CartItem> Items { get; private set; }

        public static readonly ShoppingCart Instance;

        static ShoppingCart()
        {
            if (HttpContext.Current.Session["ShoppingCart"] == null)
            {
                Instance = new ShoppingCart();
                Instance.Items = new List<CartItem>();
                HttpContext.Current.Session["ShoppingCart"] = Instance;
            }
            else
            {
                Instance = (ShoppingCart)HttpContext.Current.Session["ShoppingCart"];
            }
        }

您必须确保ShoppingCart.cs文件设置为编译。为此,右键单击文件,并在属性中设置BuildAction进行编译

using System;
using System.Data;
using System.Linq;
using WebApplication1.App_Code;
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAddShoes_Click(object sender, EventArgs e)
        {
            ShoppingCart.Instance.AddItem(1);

            Response.Redirect("ViewCart.aspx");
        }