我正在尝试访问.net核心2.0项目的剃须刀视图中的会话存储。在.net 2.0视图中是否存在与@Session"key“等效的内容?我还没有找到一个如何做到这一点的示例--我正在使用我找到的方法得到这个错误:
非静态字段、方法或属性HttpContext.Session需要对象引用。
查看:
@using Microsoft.AspNetCore.Http
[HTML button that needs to be hidden/shown based on trigger]
@section scripts {
<script>
var filteredResults = '@HttpContext.Session.GetString("isFiltered")';
</script>
}Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(30);
});
services.AddMvc();
// Added - uses IOptions<T> for your settings.
// Added - replacement for the configuration manager
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//exception handler stuff
//rewrite http to https
//authentication
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}发布于 2017-10-24 23:01:22
您可以在视图中(在ASP.NET Core2.0 :)中执行依赖注入
您应该将IHttpContextAccessor实现注入视图,并使用它从视图中获取HttpContext和Session对象。
@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor HttpContextAccessor
<script>
var isFiltered = '@HttpContextAccessor.HttpContext.Session.GetString("isFiltered")';
alert(isFiltered);
</script>假设您在Startup.cs类中有相关的代码来启用会话,这应该是可行的。
public void ConfigureServices(IServiceCollection services)
{
services.AddSession(s => s.IdleTimeout = TimeSpan.FromMinutes(30));
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}要在控制器中设置会话,您可以执行相同的操作。将IHttpContextAccessor注入控制器并使用它
public class HomeController : Controller
{
private readonly ISession session;
public HomeController(IHttpContextAccessor httpContextAccessor)
{
this.session = httpContextAccessor.HttpContext.Session;
}
public IActionResult Index()
{
this.session.SetString("isFiltered","YES");
return Content("This action method set session variable value");
}
}适当地使用会话。如果您试图传递特定于当前页面的某些数据(例如:网格数据是否被过滤,这对于当前请求非常具体),则不应该为此使用会话。考虑使用视图模型,并在可用于传递此数据的属性中使用该属性。您可以根据需要通过视图数据字典将这些值作为附加数据传递给部分视图。
请记住,Http是无状态。在添加有状态行为时,请确保您这样做是出于正确的原因。
发布于 2018-09-29 05:34:03
把这个放在剃须刀页面的顶部
@using Microsoft.AspNetCore.Http;然后,您可以轻松地访问这样的会话变量。
<h1>@Context.Session.GetString("MyAwesomeSessionValue")</h1>如果获得空值,请确保将其包含在Startup.cs中。 &确保options.CheckConsentNeeded = context设置为false 有关CheckConsentNeeded的更多信息,请查看此GDPR
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
//options.CheckConsentNeeded = context => true;
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
// Set session timeout value
options.IdleTimeout = TimeSpan.FromSeconds(30);
options.Cookie.HttpOnly = true;
});
}还要确保在配置函数中将app.UseSession();添加到应用程序管道中。
有关Asp.net核心中会话的更多信息,请查看此链接Asp.net核心中的会话
在.net内核2.1上进行测试
发布于 2017-11-02 23:39:25
正如其他人所提到的,我认为这里真正的解决办法是根本不这样做。我考虑过这个问题,虽然我有充分的理由使用该会话,但由于剃刀标记只对初始页面加载有用,所以使用存储的会话值填充控制器中的视图模型就更有意义了。
然后,可以将带有当前会话值的视图模型传递给视图,然后访问模型。那你就不用把任何东西注入到你的视野里了。
https://stackoverflow.com/questions/46921275
复制相似问题