Microsoft.AspNetCore.Mvc.Controller.TempData.get空返回

问题描述 投票:1回答:1

下面的错误msg

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Microsoft.AspNetCore.Mvc.Controller.TempData.get returned null.

我得到这个错误时:

TempData["test"] = "test"; //this is for testing
TempData["test"] = someObj; //this is my aim, but I'm getting the same error

我不知道它的问题,但我跑得类的构造函数,它是通过反射(method.Invoke(instanceConstructor, new object[] { colums });)控制器,然后方法。在该方法的逻辑,我试图用TempData

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

public class Startup
{
    public Startup(IConfiguration configuration) => Configuration = configuration;
    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            //options.CheckConsentNeeded = context => true; //this option my startup dosent know what it is. even if I copy/paste using's from github example, I just added those to the topic
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDistributedMemoryCache();
        services.AddMemoryCache();
        services.AddMvc() //.AddSessionStateTempDataProvider(); I tried with and without
        services.AddSession();
    }

    public void Configure(IApplicationBuilder app) //, IHostingEnvironment env
    {
        app.UseSession();
        app.UseAuthentication();
        app.UseMvcWithDefaultRoute();
        app.UseCookiePolicy();
      }
}

这很有趣,因为在另一个控制器的TempData工作正常。我从那些控制器检查usings和那些usings是相同的。

qazxsw POI正在与qazxsw POI的POI qazxsw它的工作没有TempDataProductController字符串类型

我发现了一些话题,其重定向我到MSDN与技巧添加TempData["message"] = $"'{deletedProduct.Name}' - produkt został usunięty"; app.UseCookiePolicy();但就像我说这不适合我

我使用的是2.0内核

请解释为什么ProductController不能正常工作或者在我的应用程序发现错误帮助

c# asp.net-core asp.net-core-2.0 tempdata
1个回答
0
投票

我不知道它的问题,但我运行类的构造函数,它是由反射的控制器,然后方法(method.Invoke(instanceConstructor,新的对象[] {colums}))。在该方法中,我试图用TempData的逻辑

它没关系! - 会话和TempData的根本不通过反射调用的方法工作。

© www.soinside.com 2019 - 2024. All rights reserved.