我丢失了什么导致[TempData]装饰的属性从获取到发布不保留值? [英] What am I missing that causes [TempData] decorated property not to retain value from get to post?
问题描述
我对asp.net核心中的TempData属性的理解是,它将模型属性标记为需要在帖子和同一页面之间保持值不变(并且寿命可能更长,但至少要更长).
My understanding of the TempData attribute in asp.net core is that it marks a model property as needing to persist in value between posts and gets of same page (and possibly wider lifetime than that, but at least that).
我的问题是,当用户回发表单时,我已将其标记为TempData并在OnGetAsync中成功设置的任何属性都已重置为null.为什么会这样?
My issue is that any property I have marked as TempData and set successfully in OnGetAsync has been reset to null by the time user posts back the form. Why might that be?
还是我误解了TempData作为属性应该做什么?如果有的话,实现我正在尝试的最佳方法是什么?将电话号码传递到视图,然后将其重新发布回OnPostAsync?!?
Or have I misunderstood what TempData is supposed to do as an attribute? If I have, what's the best way to acheive what I'm trying to do? Pass the phone number to the view and then post it back to OnPostAsync?!?
public class MyPageModel : PageModel
{
[TempData] public string PhoneNumber { get; set; }
public async Task<IActionResult> OnGetAsync(string phoneNumber)
{
PhoneNumber = phoneNumber; //THIS IS WORKING
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
user.PhoneNumber = PhoneNumber; //BUT BY HERE PHONENUMBER is NULL?
}
}
///在Startup.ConfigureServices中//我添加了以前没有的CookieTempDataProvider,但是我相信默认情况下启用了CookieTempDataProvider:
//In Startup.ConfigureServices // I added the CookieTempDataProvider which I did'nt have before, but I believe that CookieTempDataProvider is enabled by default: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata
services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
services.AddSession(options =>
{
//// Set a short timeout for easy testing.
//options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
});
有想法吗?!
推荐答案
好,我终于明白了我的问题所在.我有CookiePolicyOptions选项.即使我已取出CookieConsentPartial视图,CheckConsentNeeded lambda仍然存在.因此,我以为提供TempData支持的cookie未被设置,因为实际上我没有同意它们.
OK I finally see what my problem was. I had the CookiePolicyOptions options.CheckConsentNeeded lambda still in place, even though I had taken out the CookieConsentPartial view. So, I assume that the cookies that provided the backing for TempData were not getting set because effectively, I had not consented to them.
作为参考,整理完以下内容后,我的Configure或ConfigureServices中不需要任何与TempData或Cookie相关的内容.因为根据会话和应用程序状态在ASP.NET Core中文章:
For reference, after sorting out the below, I did not need to have ANYTHING TempData or Cookie related in my Configure or ConfigureServices. Because as per Session and app state in ASP.NET Core article:
在ASP.NET Core 2.0或更高版本中,默认情况下使用基于cookie的TempData提供程序将TempData存储在cookie中.
In ASP.NET Core 2.0 or later, the cookie-based TempData provider is used by default to store TempData in cookies.
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
// options.CheckConsentNeeded = context => true; // WORKED FINE WHEN I COMMENTED IT OUT. DID NOT WORK WHEN COMMENTED IN.
options.MinimumSameSitePolicy = SameSiteMode.None;
});
这篇关于我丢失了什么导致[TempData]装饰的属性从获取到发布不保留值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!