需要在请求期间存储静态值。 如何?

发布于 2024-07-26 16:58:26 字数 342 浏览 3 评论 0原文

我有一个 ASP.NET MVC 应用程序。 我想到了生成自动增量值以用作唯一元素 id 的想法。 问题是,我如何拥有并使用一个全局变量,该变量应该在请求期间(页面生成)存在但不再存在?

我想到使用 TempData 作为这个共享变量,然后在页面完成时删除这个键。 但是,在代码中的哪里可以清除这个 TempData 键呢? 显然,它必须是页面已经呈现的最后一段代码。

任何意见都将受到高度赞赏。

编辑:我有许多 HTML 帮助器,可以从各种视图和部分视图调用,因此在页面上声明变量并将其传递给每个帮助器显然不是一个好的解决方案。 我希望只使用帮助程序,并知道他们都在幕后获得唯一的 ID。

I have an ASP.NET MVC application. I have come to an idea of generating autoincremented values to be used as unique element ids. The question is, how can I have and work with a global variable which should be there for the duration of a request (page generation) but no longer?

I thought of using TempData for this shared variable and then just delete this key when the page is done. But then, where in code to purge this TempData key? Obviously it has to be some very last piece of code where the page has been rendered already.

Any input is highly appreciated.

EDIT: I have a number of HTML helpers that can be called from various views and partial views, so declaring a variable on a page and passing it to each helper is obviously not a good solution. I wish to just use the helpers and know they all are getting unique ids behind the scenes.

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

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

发布评论

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

评论(5

悟红尘 2024-08-02 16:58:26

好的,我用 google 搜索了一下,在 ASP.NET 论坛上找到了解决方案。

http://forums.asp.net/t/1401685.aspx

显然,我可以使用 HttpContext.Current.Items 集合在请求期间拥有我的小静态变量。

Okay, I have googled a little bit and found a solution on ASP.NET forums.

http://forums.asp.net/t/1401685.aspx

Obviously, I can use the HttpContext.Current.Items collection to have my little static variable for the duration of a request.

独﹏钓一江月 2024-08-02 16:58:26

如果您需要的只是存储一个数字,那么管理其生活方式所需的资源将比仅仅拥有一个静态整数并始终重用它需要的资源要多得多。

不必在每次请求后删除密钥。 只需使用静态(我认为这是在 Visual Basic 中共享的)整数,每次需要唯一值时使用并递增它。 每次也将其 mod 设置为高得离谱的数字,以确保它不会在单个请求中被重用,并且永远不会溢出。

If all you need is to store a number, the resources that would take to manage its lifestyle would take a lot more than just having a one static integer and always reusing it.

Do not bother deleting the key after each request. Just use a static (I think this is shared in visual basic) integer, use and increment it every time you need a unique value. Also take its mod with a ridiculously high number each time to make sure it will not be reused in a single request and it will never overflow.

花期渐远 2024-08-02 16:58:26

为什么不在页面视图文件的顶部定义整数变量?
在整个视图渲染执行过程中使用它,并且在结束时您可以轻松地保持原样。 您不必明确销毁任何东西。 您的变量仅在请求期间有效。 IIS 是无状态服务(如果减去会话、缓存和应用程序变量),因此它实际上不会明确记住任何内容。

Why don't you define your integer variable at the top of the page view file?
Use it throughout the view rendering execution and at the end of it you can easily leave it as is. You don't have to explicitly destroy anything. Your variables live for the duration of request only. IIS is stateless service (if you subtract Session, Cache and Application variables) so it doesn't really remember anything explicitly.

一曲琵琶半遮面シ 2024-08-02 16:58:26

我想你可以在 global.asax.cs 中使用 Application_BeginRequest 和 Application_EndRequest 方法; 请注意,我目前无法仔细检查方法名称,但我认为它们很接近。

I would imagine you could use the Application_BeginRequest and Application_EndRequest methods in global.asax.cs; Note I can't double check the method names currently, but I think they are close.

╰つ倒转 2024-08-02 16:58:26

您可以在控制器中创建一个成员变量,该变量将为每个请求重新生成:

public class ItemController : Controller 
{

    private int _UniqueID = 0;

    public ActionResult Index()  
    {
         foreach (var item in items)
         {
              item.UniqueID = _UniqueID++;
         }
         // etc...
    }

You could create a member variable in your controller which would be regenerated for each request:

public class ItemController : Controller 
{

    private int _UniqueID = 0;

    public ActionResult Index()  
    {
         foreach (var item in items)
         {
              item.UniqueID = _UniqueID++;
         }
         // etc...
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文