Asp.net 将键值添加到TempData会导致出现空白页
我正在使用1.0.0-rc2-final构建我的第一个.NETCoreApp。我正在尝试将模型的副本插入TempData,以便在回发后可以访问它 我已将Microsoft.AspNetCore.Session添加到我的项目中 我把Startup.cs改成了Asp.net 将键值添加到TempData会导致出现空白页,asp.net,asp.net-core,asp.net-core-mvc,Asp.net,Asp.net Core,Asp.net Core Mvc,我正在使用1.0.0-rc2-final构建我的第一个.NETCoreApp。我正在尝试将模型的副本插入TempData,以便在回发后可以访问它 我已将Microsoft.AspNetCore.Session添加到我的项目中 我把Startup.cs改成了 using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
namespace GamesCore
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public static IConfigurationRoot Configuration { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddSession();
services.AddAuthentication(
SharedOptions => SharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseCookieAuthentication();
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
ClientId = Configuration["Authentication:AzureAd:ClientId"],
Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"],
CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"]
});
app.UseSession(new SessionOptions { IdleTimeout = TimeSpan.FromMinutes(60) });
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
我的一个控制器中有以下各项:
public IActionResult Index(Models.ScoreModel scoreModel)
{
if (string.IsNullOrWhiteSpace(scoreModel.Username))
{
scoreModel.GameID = new System.Guid("b90ae557-7e03-4efa-9da1-1a4e89c1f629");
scoreModel.Username = User.Identity.Name;
scoreModel.Score = 0;
scoreModel.ScoringStep = 1;
TempData.Add("scoreModel", scoreModel);
}
return View(scoreModel);
}
当我有一行TempData在那里时,页面加载完全空白——没有错误,没有共享布局,等等。如果我删除该行,视图在共享布局中加载良好。如果我查看调试器,scoreModel将成功添加到TempData中,因此这似乎不是一个问题。我发现了这一点
我将其存储在会话中,而不是临时数据中,使用此页面作为示例:
我使用上一页概述的扩展类序列化了模型
需要注意的是,由于编写了此页面,services.AddCaching()代码>已更改为服务。AddDistributedMemoryCache()代码>
请参阅此链接以获取另一个示例:我发现了这一点
我将其存储在会话中,而不是临时数据中,使用此页面作为示例:
我使用上一页概述的扩展类序列化了模型
需要注意的是,由于编写了此页面,services.AddCaching()代码>已更改为服务。AddDistributedMemoryCache()代码>
另一个示例请参见此链接:我认为您需要将app.UseSession移动到app.useCookieAuthentication之前移动app.UseSession对该问题没有积极影响。TemDataDictionary只允许存储基元类型。如果启用运行时诊断(app.UseDeveloperExceptionPage();
)您将看到,它抱怨无法序列化基础类型。我认为您需要将app.UseSession移动到app.useCookieAuthentication之前移动app.UseSession对该问题没有积极影响。TemDataDictionary只允许存储基元类型。如果启用运行时诊断(app.usedeveloperceptionpage();
),您将看到它抱怨无法序列化底层类型。