【发布时间】:2018-07-02 17:18:24
【问题描述】:
您好,我正在尝试将对象添加到 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");
}
P.S 通读可能的解决方案我已将 app.UseSession() 添加到 Configure 方法和 services.AddServices() 在 ConfigureServices 方法中但无济于事。是否有任何我必须注意的晦涩设置?
P.S 添加ConfigureServices 和UseSession
配置服务
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?}");
});
}
【问题讨论】:
-
请发布您的 Startup.ConfigureServices 和 Startup.Configure 方法。
-
我已经发过了。
-
从不只是一个错误 500,没有任何错误消息。查看您的日志,了解实际错误 是什么。如果您找不到解决方案,请确保在您的问题中包含完整的错误消息。
-
请务必阅读how to use TempData correctly...
标签: c# asp.net-core tempdata