Asp.net Core TempData生存期和搜索词 [英] Asp.net Core TempData lifetime and search term

查看:57
本文介绍了Asp.net Core TempData生存期和搜索词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在索引中,我添加了一个搜索字段.

当用户输入搜索词并单击过滤器时,将过滤索引(Index).到现在为止还挺好.

我想实现的是,如果用户在同一控制器中执行其他操作(编辑,详细信息,删除等)并返回到索引,我希望恢复搜索.

为此,我使用了 TempData ,但没有成功.

在各种论坛/教程中,我发现生命周期存在冲突.有人说:

放置在TempData中的对象的生存期恰好是另外一个要求.

请参阅此 stackoverflow文章

在另一个网站上,我发现:

存储在TempData中的数据将一直存在于cookie中,直到您读取它为止从TempData词典中获取.

请参见文章

那么真相在哪里:只有一个子请求或当我读取TempData时?

我的测试说了第二句话:直到您读完为止" (或到期时)

这是我启动时的代码

  public void ConfigureServices(IServiceCollection服务){//[..]//仅相关代码services.AddDistributedMemoryCache();services.AddSession(options =>{//设置较短的超时时间以便于测试.options.IdleTimeout = TimeSpan.FromMinutes(15);options.CookieHttpOnly = true;});}公共无效的Configure(IApplicationBuilder应用程序,IHostingEnvironment env,ILoggerFactory loggerFactory){//[..]//仅相关代码app.UseSession();} 

在我的控制器上

 公共异步任务< IActionResult>索引(int页面,字符串搜索){搜索=搜索??TempData ["Search"] ?. ToString();//查询数据查询TempData ["search"] =搜索;} 

如果我在此控制器上进行搜索,则TempData将保存搜索项.

如果在列表中搜索后,我导航到其他页面,然后返回此处,则搜索词仍然存在

我已经知道存在. Keep ,. Peek 和其他用于管理 TempData

的方法

问题

  • 如何管理动作之间的搜索字词?
  • TempData的工作方式(直到重新读取或一次上瘾的请求)?

解决方案

从许多较早的Stack Overflow帖子和文章看来, TempData 似乎一直持续到以前版本的ASP中的下一个请求为止..网.但是,根据Microsoft的.NET Core情况并非如此:

ASP.NET Core公开Razor页面 TempData 或控制器 TempData .该属性将存储数据,直到在另一个请求中读取为止. Keep(String) Peek(string)方法可用于检查数据,而不会在请求结束时将其删除.保留将字典中的所有项目标记为保留.

我反复看到的关于 TempData 的另一个说法是如何使用会话来实现它,但这不是.NET Core的默认机制:

默认情况下启用基于cookie的 TempData 提供程序.要启用基于会话的 TempData 提供程序,请使用 AddSessionStateTempDataProvider 扩展方法.

来源: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-3.1#tempdata

In my index I added a search field.

When user enter a search term and click filter the index (Index) is filtered. So far so good.

What I would like to achieve is that if the user performs other operations (edit, details, delete, etc) in the same controller and returns to the Index, I would like the search to be restored.

To do this, I used TempData but without success.

In the various forums / tutorials I found conflicting about lifetime. Some say:

lifetime of an object placed in TempData is exactly one additional request.

See this stackoverflow article

On another site i found:

Data stored in TempData will be alive in the cookie until you read it from the TempData dictionary.

See this article

So where is the truth: Only one sub request or when I read the TempData ?

My tests says the second: "until you read it" (or when expire)

Here my code on startup

public void ConfigureServices(IServiceCollection services)
{
    // [..]
    // Relevant code only
    services.AddDistributedMemoryCache();

    services.AddSession(options =>
    {
        // Set a short timeout for easy testing.
        options.IdleTimeout = TimeSpan.FromMinutes(15);
        options.CookieHttpOnly = true;
    });

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{

    // [..]
    // Relevant code only

    app.UseSession();

}

On my controller

public async Task<IActionResult> Index(int page, string search)
{
    search = search ?? TempData["Search"]?.ToString();
    // Query with search data

    TempData["search"] = search;
}

If I search on this controller the TempData save the search term.

If, after searching in the list, I navigate to other pages and then return here, the search term is still present

I already know that exist .Keep, .Peek and other methods for manage the TempData

Questions

  • How manage the search term between actions ?
  • How work the TempData (until re-read or on ONE addictional request) ?

解决方案

Judging by many older Stack Overflow posts and articles out there, it seems TempData only lasted until the next request in prior versions of ASP.NET. However, that is not the case as of .NET Core according to Microsoft:

ASP.NET Core exposes the Razor Pages TempData or Controller TempData. This property stores data until it's read in another request. The Keep(String) and Peek(string) methods can be used to examine the data without deletion at the end of the request. Keep marks all items in the dictionary for retention.

Another claim I see repeatedly about TempData is how it is implemented using sessions, but that isn't the default mechanism as of .NET Core:

The cookie-based TempData provider is enabled by default. To enable the session-based TempData provider, use the AddSessionStateTempDataProvider extension method.

Source: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-3.1#tempdata

这篇关于Asp.net Core TempData生存期和搜索词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆