【发布时间】:2021-04-06 19:00:14
【问题描述】:
我的服务器使用 Cloud SQL 在 .NET Core 3.1 MVC 上运行 - 但无法在控制器之间传递任何数据。尝试了 HttpContext.Session 和 TempData - 重定向后都变为 Null。
有人看过吗?
代码如下:
public void ConfigureServices(IServiceCollection 服务) { services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(60); // Set the session expired time
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
services.AddControllersWithViews();
services.AddHttpContextAccessor();
// Stop annoying startup messages
services.Configure<ConsoleLifetimeOptions>(opts => opts.SuppressStatusMessages = true);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=User}/{action=Login}/");
});
}
【问题讨论】:
-
可能是会话过期的问题,请查看ConfigureServices方法,当你configure session state时,你设置过会话过期时间吗?尝试延长会话过期时间。如果还是不行,最好分享下相关代码。
-
我将过期时间设置为 60 分钟。
-
我应该提到,当我部署到本地 IIS 服务器时,它可以正常工作。只有当我上传到 GAE 时它才会失败。
-
"只有上传到GAE才会失败" 如果是这样的话,可能是GAE配置的问题,我想你最好联系GAE论坛。来自Google cloud document,建议使用Firestore处理会话状态,可以参考。
标签: google-cloud-platform asp.net-core-mvc