你好,我试图将一个对象添加到TempData
并重定向到另一个控制器动作。在使用TempData
时,我遇到了500错误消息。
public IActionResult Attach(long Id)
{
Story searchedStory=this.context.Stories.Find(Id);
if(searchedStory!=null)
{
TempData["tStory"]=searchedStory; //JsonConvert.SerializeObject(searchedStory) gets no error and passes
return RedirectToAction("Index","Location");
}
return View("Index");
}
public IActionResult Index(object _story)
{
Story story=TempData["tStory"] as Story;
if(story !=null)
{
List<Location> Locations = this.context.Locations.ToList();
ViewBag._story =JsonConvert.SerializeObject(story);
ViewBag.rstory=story;
return View(context.Locations);
}
return RedirectToAction("Index","Story");
}
顺便提一下,阅读可能的解决方案后,我在Configure
方法中添加了app.UseSession()
,在ConfigureServices
方法中添加了services.AddServices()
,但都没有奏效。是否有我必须注意的模糊设置?
顺便说一下,添加ConfigureServices
和UseSession
ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddDbContext<TreasureContext>(x=>x.UseMySql(connectionString:Constrings[3]));
services.AddMvc();
services.AddSession();
}
配置
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}