下面的错误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它的工作没有TempData
太ProductController
字符串类型
我发现了一些话题,其重定向我到MSDN与技巧添加TempData["message"] = $"'{deletedProduct.Name}' - produkt został usunięty";
和app.UseCookiePolicy();
但就像我说这不适合我
我使用的是2.0内核
请解释为什么ProductController
不能正常工作或者在我的应用程序发现错误帮助
我不知道它的问题,但我运行类的构造函数,它是由反射的控制器,然后方法(method.Invoke(instanceConstructor,新的对象[] {colums}))。在该方法中,我试图用TempData的逻辑
它没关系! - 会话和TempData的根本不通过反射调用的方法工作。