相关疑难解决方法(0)

访问Asp Core 2 View中的Session对象

我想在视图中显示Session.那可能吗?我在我看来尝试这个

<div class="content-header col-xs-12">
   <h1>Welcome, @HttpContext.Session.GetString("userLoggedName")</h1>
</div>
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误

严重级代码描述项目文件行抑制状态错误 CS0120 非静态字段,方法或属性"HttpContext.Session"需要对象引用

任何帮助,我将不胜感激.谢谢

c# asp.net-mvc razor asp.net-core asp.net-core-2.0

6
推荐指数
1
解决办法
2453
查看次数

无法访问我的Asp.Net Core项目中的帮助器类内的Session对象

我正在尝试在ASP.NET Core 2.1项目中的帮助器类中访问HttpContext.Session对象。

当我尝试访问HttpContext.Session时,出现以下错误。

CS0120非静态字段,方法或属性'HttpContext.Session'需要对象引用

使用.NET 4.x ASP.NET,可以使用“ HttpContext.Current.Session”轻松访问它。

这是我的课:

 public class MySession
 {

    public void Foo()
    {
        HttpContext.Session.SetString("Name", "The Doctor"); // will not work
        HttpContext.Session.SetInt32("Age", 773);  // will not work

    }
 }
Run Code Online (Sandbox Code Playgroud)

这是我的Startup.cs:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDistributedMemoryCache();

            services.AddSession(options =>
            {
                // Set …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-2.0

2
推荐指数
1
解决办法
1472
查看次数

标签 统计

asp.net-core-2.0 ×2

c# ×2

asp.net-core ×1

asp.net-mvc ×1

razor ×1