这是错误的全文(编译时没有错误。我无法访问此页面):
你好。我的购物车有问题。我不知道如何解决它。请帮忙。
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();
}
}
}
ShopCartController
在构造函数中接受一个参数TicketRepository
:在 DI 容器中,只有
IAllTicket
:所以要么在构造函数中更改参数的类型,要么在 DI 容器中更改参数的类型。
注意:
ShopCart
钉在IServiceProvider
. 实际上,这里使用的不是依赖注入模式,而是服务定位器反模式。DI 背后的主要思想是摆脱外部依赖。然后直接依赖于特定的 DI 容器。
尝试用任何其他替换它 - 使用正确的 DI 实现,仅替换 Composition Root 代码就足够了 - 设置依赖关系的地方:所有这些
AddSingleton
,AddTransient
,AddScoped
.而且您还必须重写代码
ShopCart
。