如何将对象从过滤器传递到操作方法?
How do I pass an object from a filter to an action method?
我在 ASP.NET 核心应用程序中实施购物车时遇到了一些问题。我正在使用会话存储来执行此操作,但每次 OnActionExecuted
执行时,传递给过滤器的购物车对象都是空的。有人知道为什么吗?
会话过滤器 class:
public class WinkelmandSessionFilter : ActionFilterAttribute
{
private Winkelmand _mand;
public WinkelmandSessionFilter()
{
}
public override void OnActionExecuting(ActionExecutingContext context)
{
_mand = ReadCartFromSession(context.HttpContext);
context.ActionArguments["cart"] = _mand;
base.OnActionExecuting(context);
}
public override void OnActionExecuted(ActionExecutedContext context)
{
WriteCartToSession(_mand, context.HttpContext);
base.OnActionExecuted(context);
}
private Winkelmand ReadCartFromSession(HttpContext context)
{
Winkelmand cart = context.Session.GetString("cart") == null ?
new Winkelmand() : JsonConvert.DeserializeObject<Winkelmand>(context.Session.GetString("cart"));
return cart;
}
private void WriteCartToSession(Winkelmand cart, HttpContext context)
{
context.Session.SetString("cart", JsonConvert.SerializeObject(cart));
}
}
使用该过滤器的方法:
[ServiceFilter(typeof(WinkelmandSessionFilter))]
public IActionResult BonEdit(Winkelmand mand, NieuwViewModel model)
{
var bon = new Bon();
bon.NaamGeadreseerde = model.naamGeadreseerde;
bon.EmailGeadreseerde = model.emailGeadreseerde;
bon.NaamGever = model.naamGever;
bon.Bedrag = model.Bedrag;
bon.Boodschap = model.Boodschap;
bon.Winkel = model.Winkel;
bon.BonId = Guid.NewGuid().GetHashCode();
mand.bonToevoegen(bon);
bon.genereerPdf();
return RedirectToAction(nameof(BonVoorbeeld), bon);
}
您正在将购物车传递给使用名称 cart
的操作:
context.ActionArguments["cart"] = _mand;
但是当您从操作方法访问它时,名称是 mand
:
public IActionResult BonEdit(Winkelmand mand, NieuwViewModel model)
为了将其传递到操作方法中,这 2 个名称必须匹配。
context.ActionArguments["mand"] = _mand;
我在 ASP.NET 核心应用程序中实施购物车时遇到了一些问题。我正在使用会话存储来执行此操作,但每次 OnActionExecuted
执行时,传递给过滤器的购物车对象都是空的。有人知道为什么吗?
会话过滤器 class:
public class WinkelmandSessionFilter : ActionFilterAttribute
{
private Winkelmand _mand;
public WinkelmandSessionFilter()
{
}
public override void OnActionExecuting(ActionExecutingContext context)
{
_mand = ReadCartFromSession(context.HttpContext);
context.ActionArguments["cart"] = _mand;
base.OnActionExecuting(context);
}
public override void OnActionExecuted(ActionExecutedContext context)
{
WriteCartToSession(_mand, context.HttpContext);
base.OnActionExecuted(context);
}
private Winkelmand ReadCartFromSession(HttpContext context)
{
Winkelmand cart = context.Session.GetString("cart") == null ?
new Winkelmand() : JsonConvert.DeserializeObject<Winkelmand>(context.Session.GetString("cart"));
return cart;
}
private void WriteCartToSession(Winkelmand cart, HttpContext context)
{
context.Session.SetString("cart", JsonConvert.SerializeObject(cart));
}
}
使用该过滤器的方法:
[ServiceFilter(typeof(WinkelmandSessionFilter))]
public IActionResult BonEdit(Winkelmand mand, NieuwViewModel model)
{
var bon = new Bon();
bon.NaamGeadreseerde = model.naamGeadreseerde;
bon.EmailGeadreseerde = model.emailGeadreseerde;
bon.NaamGever = model.naamGever;
bon.Bedrag = model.Bedrag;
bon.Boodschap = model.Boodschap;
bon.Winkel = model.Winkel;
bon.BonId = Guid.NewGuid().GetHashCode();
mand.bonToevoegen(bon);
bon.genereerPdf();
return RedirectToAction(nameof(BonVoorbeeld), bon);
}
您正在将购物车传递给使用名称 cart
的操作:
context.ActionArguments["cart"] = _mand;
但是当您从操作方法访问它时,名称是 mand
:
public IActionResult BonEdit(Winkelmand mand, NieuwViewModel model)
为了将其传递到操作方法中,这 2 个名称必须匹配。
context.ActionArguments["mand"] = _mand;