简体   繁体   English

在ASP.NET Core中引用使用System.Web.HttpContext.Current的ASP.NET Framework 4.6.2

[英]Referencing ASP.NET framework 4.6.2 in ASP.NET Core, Which use System.Web.HttpContext.Current

I have a class in ASP.Net framework 4.6 SessionHelper Code of class is as follow 我在ASP.Net Framework 4.6 SessionHelper中有一个类,该类的代码如下

using System;
using System.Web;
using MyPortal.Common.Entity;
using MyPortal.Common.Helper;

namespace MyPortal.BusinessLayer.Helper
{
    public static class SessionHelper
    {
        public static User GetLoggedInUser { get { return (User) GetFromSession(User.SESSION_LOGGEDIN_USER); } }
        public static User GetLoggedInExternalUser { get { return (User)GetFromSession(User.SESSION_LOGGEDIN_EXTERNAL_USER); } }

        public static string GetValueFromSession(string sessionKey)
        {
            return HttpContext.Current.Session[sessionKey] == null ? string.Empty : HttpContext.Current.Session[sessionKey].ToString();
        }

        public static void SaveInSession(string sessionKey, object sessionValue)
        {
            HttpContext.Current.Session[sessionKey] = sessionValue;
        }

        public static void RemoveSession(string sessionKey)
        {
            if (HttpContext.Current.Session[sessionKey]!=null)
                HttpContext.Current.Session.Remove(sessionKey);
        }

    }
}

Now This SessionHelper class is used in many places MyPortal.BusinessLayer, this project or dll is running fine in a ASP.NET web project. 现在,此SessionHelper类可在MyPortal.BusinessLayer的许多地方使用,该项目或dll在ASP.NET Web项目中运行良好。

Now i have to use this BusinessLayer.dll in ASP.NET Core project, and want to access some of the methods, like getting loggedInUserBalance these methods in BusinessLayer.dll are using SessionHelper which itself using HttpContext.Current.Session 现在,我必须在ASP.NET Core项目中使用此BusinessLayer.dll,并想访问某些方法,例如获取loginInUserBalance,在BusinessLayer.dll中,这些方法都使用SessionHelper,而SessionHelper本身使用HttpContext.Current.Session

Now I am getting error in ASP.NET Core 现在我在ASP.NET Core中遇到错误

System.TypeLoadException HResult=0x80131522 Message=Could not load type 'System.Web.HttpContext' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. System.TypeLoadException HResult = 0x80131522消息=无法从程序集'System.Web,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'中加载类型'System.Web.HttpContext'。

The ideal way is to use IHttpContextAccessor. 理想的方法是使用IHttpContextAccessor。

In your startup you can write a code like this: 在启动时,您可以编写如下代码:

public void ConfigureServices(IServiceCollection services)
{
     services.AddMvc()
         .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
     services.AddHttpContextAccessor();
     services.AddTransient<IUserRepository, UserRepository>();
}

Then whereever you want to use HttpContext, you can inject IHttpContextAccesor as shown in below example: 然后,无论您想使用HttpContext到哪里,都可以注入IHttpContextAccesor,如下例所示:

public class UserRepository : IUserRepository
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public UserRepository(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public void LogCurrentUser()
    {
        var username = _httpContextAccessor.HttpContext.User.Identity.Name;
        service.LogAccessRequest(username);
    }
}

This documentation article explains about accessing httpcontext from various places. 本文档文章介绍了有关从各个位置访问httpcontext的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET Core 中访问当前的 HttpContext - Access the current HttpContext in ASP.NET Core 如何在ASP.NET Core中使用HttpContext.Current - How to use HttpContext.Current in ASP.NET Core VS 2017 .NET 4.6.2新的ASP.NET核心Web应用程序(.NET Framework)默认为x86平台 - VS 2017 .NET 4.6.2 new ASP.NET Core Web Application (.NET Framework) defaults to x86 platform ASP.NET中的System.Web.HttpContext.Current.User.Identity.Name与System.Environment.UserName的对比 - System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET 通过自定义访问ASP.NET Core中的当前HttpContext - Access the current HttpContext in ASP.NET Core with Custom ASP.NET Web Api中HttpContext.Current.Items的替代方法 - Alternative to HttpContext.Current.Items in ASP.NET Web Api System.Web.HttpContext.Current.get在ASP.NET MVC控制器中返回null - System.Web.HttpContext.Current.get returned null in asp.net mvc controller 在 ASP.NET Core3.1 中使用包含“System.Web.HttpContext”的旧项目 - Using legacy projects containing 'System.Web.HttpContext' in ASP.NET Core3.1 ASP.NET WebForms - 在当前 HttpContext 中创建和使用新会话 - ASP.NET WebForms - Create and use a new session in current HttpContext Asp.Net核心HTTPContext和RewritePath - Asp.Net Core HTTPContext and RewritePath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM