gpt4 book ai didi

asp.net-mvc - TempData 始终为空

转载 作者:行者123 更新时间:2023-12-02 03:22:37 25 4
gpt4 key购买 nike

我想使用 TempData 来存储 Post 和后续重定向之间的消息,但 TempData 始终为空。

我有 BaseContoller 提供一些用于传递 TempData 的基础设施。简化的代码如下所示:

public abstract class BaseController : Controller
{
public const string AuditMessagesKey = "AuditMessages";

private List<InformationMessage> _informationMessages = new List<InformationMessage>();

protected BaseController()
{
// I also tried this in overriden Initialize
ViewData[AuditMessagesKey] = GetAuditMessages();
}

protected void AddAuditMessage(InformationMessage message)
{
if (message == null)
return;

_informationMessages.Add(message);
}

protected override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);

if (filterContext.Result is RedirectToRouteResult)
{
// I see that messages are stored into TempData
TempData[AuditMessagesKey] = _informationMessages;
// This also doesn't help
// TempData.Keep(AuditMessagesKey);
}
}

private ICollection<InformationMessage> GetAuditMessages()
{
// TempData are always empty here
var messages = TempData[AuditMessagesKey] as List<InformationMessage>;

if (messages == null)
{
messages = new List<InformationMessage>();
}

return messages;
}
}

操作方法如下:

  [HttpPost]
public ActionResult CancelEdit(RequestSaveModel model)
{
AddAuditMessage(new InformationMessage
{
Message = String.Format(Messages.RequestEditationCanceled, model.Title),
Severity = MessageSeverity.Information
});

return RedirectToAction("Detail", new { Id = model.Id});
}

应用程序已在 VS Development Web 服务器上进行测试。没有 Ajax 调用,并且我从母版页中删除了所有 Html.RenderAction 调用。我可以看到,在 GetAuditedMessages 中每个请求仅访问 TempData 一次,并且在 OnResultExecuting 中仅存储一次。没有任何内容会覆盖数据。允许 session 状态。

代码稍微简化了一些。我们还使用防伪 token 、自定义过滤器进行授权和操作选择,但它不应影响 TempData 行为。

我不明白。我之前在测试应用程序中使用过 TempData,效果很好。

最佳答案

我在代码中看到的问题是,您试图在 Controller 的构造函数中从 TempData 检索数据 - 这是在数据可用之前。

将对 GetAuditMessages() 的调用移至 OnActionExecuting 方法中,这样就可以访问该方法了。

public abstract class BaseController : Controller
{
public const string AuditMessagesKey = "AuditMessages";

private List<InformationMessage> _informationMessages = new List<InformationMessage>();

protected BaseController()
{
// TempData is not available yet
}

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
ViewData[AuditMessagesKey] = GetAuditMessages();

base.OnActionExecuting(filterContext);
}

protected void AddAuditMessage(InformationMessage message)
{
if (message == null)
return;

_informationMessages.Add(message);
}

protected override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);

if (filterContext.Result is RedirectToRouteResult)
{
// I see that messages are stored into TempData
TempData[AuditMessagesKey] = _informationMessages;
// This also doesn't help
// TempData.Keep(AuditMessagesKey);
}
}

private ICollection<InformationMessage> GetAuditMessages()
{
var messages = TempData[AuditMessagesKey] as List<InformationMessage>;

if (messages == null)
{
messages = new List<InformationMessage>();
}

return messages;
}
}

关于asp.net-mvc - TempData 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4681092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com