简体   繁体   English

Session 和应用程序池:存储和显示图像的 ASP.NET 应用程序

[英]Session and app pool: an ASP.NET app that stores and displays images

I have a .NET app that allows users to upload images to a directory within the webapp and then view them.我有一个 .NET 应用程序,允许用户将图像上传到 web 应用程序内的目录,然后查看它们。 The problem is that the session gets lost when I upload or delete an image in the webapp directory.问题是当我在 webapp 目录中上传或删除图像时,session 会丢失。 It seems that the app pool is getting recycled when I add images, and not just config or cs files.当我添加图像时,应用程序池似乎正在被回收,而不仅仅是配置或 cs 文件。

I have seen this technique used in so many tutorials that I wonder if it does actually work with the right server settings, or if it is a completely flawed technique.我已经在很多教程中看到过这种技术,我想知道它是否确实适用于正确的服务器设置,或者它是否是一种完全有缺陷的技术。

If I add an image to a subdirectory manually, or delete it manually, the session remains.如果我手动将图像添加到子目录或手动删除它,session 仍然存在。 If I add the image to a subdirectory through visual studio the session remains, but if I delete it through visual studio, the session is lost.如果我通过 Visual Studio 将图像添加到子目录,则 session 仍然存在,但如果我通过 Visual Studio 将其删除,则 session 将丢失。

If I upload the images to a folder outside of the webapp then I can't show them in img tags.如果我将图像上传到 webapp 之外的文件夹,那么我无法在 img 标签中显示它们。

I'd be interested to hear what you might do as a workaround.我很想听听您可能会采取什么解决方法。

Try storing the Session in an out of state process.尝试将 Session 存储在 state 进程之外。 You can do this by starting the "ASP.Net State Service" in the Services screen of "Administrative Tools" and then using this line in your web.config:您可以通过在“管理工具”的“服务”屏幕中启动“ASP.Net State 服务”,然后在 web.config 中使用此行来执行此操作:

<sessionState
            mode="StateServer"
            stateConnectionString="tcpip=127.0.0.1:42424"
            cookieless="false"
            timeout="20" />

This will allow your sessions to persist, even if you reset IIS.这将允许您的会话持续存在,即使您重置 IIS。

I'd check the event log to make sure something you are doing isn't crashing the AppPool我会检查事件日志以确保您正在做的事情不会使 AppPool 崩溃

You can store information such as an Image ID in an asp:HiddenField or as a.Attribute[] in a control.您可以将诸如图像 ID 之类的信息存储在 asp:HiddenField 或控件中的 a.Attribute[] 中。 When I need to bind a button to a specific item in a list, for example, I give the button an Attribute that reflects the ID of the item in question.例如,当我需要将按钮绑定到列表中的特定项目时,我会为按钮提供一个反映相关项目 ID 的属性。 I use HiddenFields for other info that doesn't have one associated control (ie Post IDs, etc).我将 HiddenFields 用于没有关联控件的其他信息(即帖子 ID 等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM