这是错误的全文(编译时没有错误。我无法访问此页面):
你好。我的购物车有问题。我不知道如何解决它。请帮忙。
using Microsoft.EntityFrameworkCore;
using WebApplication1;
using WebApplication1.Interface;
using WebApplication1.mocks;
using WebApplication1.Models;
using WebApplication1.Repository;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("dbsettings.json").Build();
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<AppDBContent>(x => x.UseSqlServer(connectionString));
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddTransient<IAllTicket, TicketRepository>();
builder.Services.AddTransient<ITicketCategory, CategoryRepository>();
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddScoped(sp => ShopCart.GetCart(sp));
builder.Services.AddMvc();
builder.Services.AddMemoryCache();
builder.Services.AddSession();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSession();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
using (var scope = app.Services.CreateScope())
{
AppDBContent content = scope.ServiceProvider.GetRequiredService<AppDBContent>();
DBObject.Initial(content);
}
app.Run();
控制器本身的代码
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
using WebApplication1.Repository;
using WebApplication1.ViewModels;
namespace WebApplication1.Controllers
{
public class ShopCartController : Controller
{
private readonly TicketRepository _gameRep;
private readonly ShopCart _shopCart;
public ShopCartController(TicketRepository gameRep, ShopCart gameCart)
{
_gameRep = gameRep;
_shopCart = gameCart;
}
public ViewResult Index()
{
var items = _shopCart.getShopItems();
_shopCart.listShopItems = items;
var obj = new ShopCartViewModel
{
shopCart = _shopCart
};
return View(obj);
}
public RedirectToActionResult addtoCart(int id)
{
var item = _gameRep.Tickets.FirstOrDefault(i => i.Id == id);
if(item != null)
{
_shopCart.AddtoCart(item);
}
return RedirectToAction("Index");
}
}
}
模型代码
using Microsoft.EntityFrameworkCore;
namespace WebApplication1.Models
{
public class ShopCart
{
private readonly AppDBContent appDBContent;
public ShopCart(AppDBContent appDBContent)
{
this.appDBContent = appDBContent;
}
public string ShopCartId { get; set; }
public List<ShopCartItem> listShopItems { get; set; }
public static ShopCart GetCart(IServiceProvider services)
{
ISession session = services.GetRequiredService<IHttpContextAccessor>()?.HttpContext.Session;
var context = services.GetService<AppDBContent>();
string shopCartid = session.GetString("Cartid") ?? Guid.NewGuid().ToString();
session.SetString("Cartid", shopCartid);
return new ShopCart(context) { ShopCartId = shopCartid };
}
public void AddtoCart(Ticket ticket)
{
appDBContent.ShopCartItem.Add(new ShopCartItem
{
ShopCartId = ShopCartId,
ticket = ticket,
price = ticket.Price,
});
appDBContent.SaveChanges();
}
public List<ShopCartItem> getShopItems()
{
return appDBContent.ShopCartItem.Where(c => c.ShopCartId == ShopCartId).Include(s => s.ticket).ToList();
}
}
}