告诉操作/控制器保留 TempData

发布于 2024-08-09 17:43:31 字数 243 浏览 2 评论 0原文

据我了解,TempData 设计为仅在单页请求之间工作。但我认为有一种情况破坏了预期的功能。

我优先使用控制器而不是处理程序来传递图像。我现在不知道这是否是最佳实践,但它对我来说非常有效。但问题是,对图像操作之一的每次调用显然都会耗尽 TempData 信用。

MVC 中有没有办法说“此控制器/操作超出了正常页面请求的范围”,因此要么保留 TempData,要么将其自身完全从 TempData 生命周期中删除?

富有的

I understand that TempData is designed to work only between a single page request. But I think have a situation I think breaks the intended functionality.

I use Controllers in preference to Handlers for delivering images. I don't now if that is best practice, but it works very well for me. The problem though, is that each call to one of the image Actions obviously uses up a TempData credit.

Is there a way in MVC to say "This Controller/Action is out of the scope of normal page requests" and therefore either persist the TempData or remove itself from the TempData life cycle completely?

Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

香橙ぽ 2024-08-16 17:43:31

我的解决方案是创建一个在页面请求之间保留 TempData 的属性。我对此的最初反应是“恶心”,但我想有效地从 TempData 生命周期中排除任何用该属性修饰的控制器。

using System.Web.Mvc;

namespace K3R.Web.Mvc.Filters {
    public sealed class PersistTempDataAttribute : ActionFilterAttribute {
        public PersistTempDataAttribute() { }

        public override void OnActionExecuting(ActionExecutingContext filterContext) {
            var tempData = filterContext.Controller.TempData;
            if (tempData == null || tempData.Count == 0)
                return;

            string[] keys = new string[tempData.Keys.Count];
            tempData.Keys.CopyTo(keys, 0);
            foreach (var key in keys)
                tempData[key] = tempData[key];
        }
    }
}

任何有关更好解决方案的反馈将不胜感激。

富有的

My solution has been to create an Attribute that persists the TempData across page requests. My initial reaction to this is "yuck", but I want to effectively exclude any controllers decorated with the Attribute from the TempData lifecycle.

using System.Web.Mvc;

namespace K3R.Web.Mvc.Filters {
    public sealed class PersistTempDataAttribute : ActionFilterAttribute {
        public PersistTempDataAttribute() { }

        public override void OnActionExecuting(ActionExecutingContext filterContext) {
            var tempData = filterContext.Controller.TempData;
            if (tempData == null || tempData.Count == 0)
                return;

            string[] keys = new string[tempData.Keys.Count];
            tempData.Keys.CopyTo(keys, 0);
            foreach (var key in keys)
                tempData[key] = tempData[key];
        }
    }
}

Any feedback on a better solution would be appreciated.

Rich

合久必婚 2024-08-16 17:43:31

我不确定这个问题的确切答案,因为我自己对 MVC 还很陌生。然而,我听说有人在处理 AJAX 请求时遇到麻烦,并且在他们想要之前就终止了 TempData。

我想您可以实现自己的基于会话的系统来存储这些信息?

不过,我相信其他人很快就会为您提供更完整的答案!

I'm not sure of the exact answer to this as I'm pretty new to MVC myself. However, I have heard of people having trouble with AJAX requests killing off the TempData before they wanted it to as well.

I guess you could implement your own Session based system to store this information?

I'm sure someone else will have a more complete answer for you soon, though!

自由如风 2024-08-16 17:43:31

我不完全理解你的做法和原因,但我宁愿选择处理程序。或者,如果您坚持使用控制器操作,我可以说这对我来说效果很好,没有任何动态传递图像的问题,并且我在控制器操作中使用了 FileContentResult。

像这样:

        return File(imageBytes, imageType);

您从数据存储区或其他地方获取字节..

希望它有帮助

I do not completely understand your way of doing it and why but I would rather go with handlers. Or if you stick to controller actions, I can say that this worked for me fine without any issues for delivering images on-the-fly and I used FileContentResult in my controller actions.

Like this:

        return File(imageBytes, imageType);

You get the bytes out of datastore or somewhere..

hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文