向TempData添加键值会导致空白页面

时间:2016-06-03 06:40:07

标签: 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.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行时,页面加载完全空白 - 没有错误,没有共享布局等。如果我删除该行,View在共享布局中加载正常。如果我查看调试器,则scoreModel会成功添加到TempData中,这似乎不是问题。

1 个答案:

答案 0 :(得分:0)

我想出来了。

我转而将其存储在Session而不是TempData中,使用此页面作为示例:

http://benjii.me/2015/07/using-sessions-and-httpcontext-in-aspnet5-and-mvc6/

我使用上页概述的扩展类序列化了Model。

需要注意的是,自从撰写此页面后,services.AddCaching();已更改为services.AddDistributedMemoryCache();

请参阅此链接以获取另一个示例:https://github.com/aspnet/Session/blob/dev/samples/SessionSample/Startup.cs