【发布时间】:2017-01-08 00:47:39
【问题描述】:
我们正在关注这个 ASP.NET 官方文档:Managing Application State。在我们的控制器中,我们正在为HttpContext.Items[...] 设置一个值,并尝试从相应视图调用的ViewComponent 访问该值。但是我们在ViewComponent 中将HttpContext.Items[...] 设为null。
控制器:
HttpContext.Items["TestVal"]= "some value";
查看:
@await Component.InvokeAsync("myVC")
视图组件:
public class myVCViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync()
{
String myVal= Http.Items["TestVal"].ToString(); //issue: Http.Items["TestVal"] is null at this line
return View(items);
}
}
更新:
在上面的控制器部分中,将 Http.Items 更改为 HttpContext.Items 在以下行中:HttpContext.Items["TestVal"]= "some value";
【问题讨论】:
标签: c# asp.net-core asp.net-core-viewcomponent