Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 如何在ASP.NET Core中获取HttpContext.Current.Session?_Asp.net Core - Fatal编程技术网

Asp.net core 如何在ASP.NET Core中获取HttpContext.Current.Session?

Asp.net core 如何在ASP.NET Core中获取HttpContext.Current.Session?,asp.net-core,Asp.net Core,我需要将MVC项目迁移到.net Core,我知道它已从ASP.net Core中删除System.Web。 我需要转换 HttpContext.Current.Session[“name”]!=asp.net核心处为空。 我补充说: 使用Microsoft.AspNetCore.Http 但是我有一个错误。您是否测试过Microsoft,示例如下所示 public const string SessionKeyName = "_Name"; public const string Sessio

我需要将MVC项目迁移到.net Core,我知道它已从ASP.net Core中删除System.Web。 我需要转换 HttpContext.Current.Session[“name”]!=asp.net核心处为空。 我补充说: 使用Microsoft.AspNetCore.Http 但是我有一个错误。

您是否测试过Microsoft,示例如下所示

public const string SessionKeyName = "_Name";
public const string SessionKeyAge = "_Age";
const string SessionKeyTime = "_Time";

 // Requires: using Microsoft.AspNetCore.Http;
    if (string.IsNullOrEmpty(HttpContext.Session.GetString(SessionKeyName)))
    {
        HttpContext.Session.SetString(SessionKeyName, "The Doctor");
        HttpContext.Session.SetInt32(SessionKeyAge, 773);
    }

    var name = HttpContext.Session.GetString(SessionKeyName);
    var age = HttpContext.Session.GetInt32(SessionKeyAge);
您是否测试过Microsoft,示例如下所示

public const string SessionKeyName = "_Name";
public const string SessionKeyAge = "_Age";
const string SessionKeyTime = "_Time";

 // Requires: using Microsoft.AspNetCore.Http;
    if (string.IsNullOrEmpty(HttpContext.Session.GetString(SessionKeyName)))
    {
        HttpContext.Session.SetString(SessionKeyName, "The Doctor");
        HttpContext.Session.SetInt32(SessionKeyAge, 773);
    }

    var name = HttpContext.Session.GetString(SessionKeyName);
    var age = HttpContext.Session.GetInt32(SessionKeyAge);
这样使用:

HttpContext.Session.SetString("priceModel", JsonConvert.SerializeObject(customobject));
var priceDetails = HttpContext.Session.GetString("priceModel");
在startup类中确保以下几点:

  • ConfigureServices方法中的AddSession

    services.AddSession();
    
  • 在配置方法中使用会话:

    app.UseSession();
    
  • 这样使用:

    HttpContext.Session.SetString("priceModel", JsonConvert.SerializeObject(customobject));
    var priceDetails = HttpContext.Session.GetString("priceModel");
    
    在startup类中确保以下几点:

  • ConfigureServices方法中的AddSession

    services.AddSession();
    
  • 在配置方法中使用会话:

    app.UseSession();
    

  • 推荐的方法是使用内置的依赖项注入容器注册依赖项。将IHttpContextAccessor注入到相应的服务中

    public class UserRepository : IUserRepository
        {
            private readonly IHttpContextAccessor _httpContextAccessor;
    
            public UserRepository(IHttpContextAccessor httpContextAccessor)
            {
                _httpContextAccessor = httpContextAccessor;
            }
    
            public void LogCurrentUser()
            {
                var username = _httpContextAccessor.HttpContext.Session.GetString("UserName");
                service.LogAccessRequest(username);
            }
        }
    

    有关更多详细信息,请参阅

    推荐的方法是使用内置依赖项注入容器注册依赖项。将IHttpContextAccessor注入到相应的服务中

    public class UserRepository : IUserRepository
        {
            private readonly IHttpContextAccessor _httpContextAccessor;
    
            public UserRepository(IHttpContextAccessor httpContextAccessor)
            {
                _httpContextAccessor = httpContextAccessor;
            }
    
            public void LogCurrentUser()
            {
                var username = _httpContextAccessor.HttpContext.Session.GetString("UserName");
                service.LogAccessRequest(username);
            }
        }
    

    有关更多详细信息,请参阅

    简单的谷歌搜索可以为您提供这些信息,无论如何,可以通过控制器中的
    request.httpcontext
    和视图中的
    context
    来访问这些信息。在控制器中,它可以工作,但在服务级别上它不工作。@user3296338 services.AddSession();app.UseSession();在startup类中添加?一个简单的google搜索可以为您提供这些信息,无论如何,可以通过控制器中的
    request.httpcontext
    和视图中的
    context
    来访问这些信息。在控制器中它可以工作,但在服务级别它不工作。@user3296338 services.AddSession();app.UseSession();在启动类中添加?在控制器中它工作,但在服务级别它不工作在控制器中它工作,但在服务级别它不工作