【问题标题】:Access the current HttpContext in ASP.NET Core with Custom使用自定义访问 ASP.NET Core 中的当前 HttpContext
【发布时间】:2019-03-21 18:12:48
【问题描述】:

我正在编写以下代码:Access the current HttpContext in ASP.NET Core

我收到错误消息。我将如何解决这个问题? 另外,接口IMyComponent 的代码是什么?只是想确定它的正确性。

错误:

类型或命名空间 IMyComponent 找不到 当前上下文中不存在名称“KEY”。

public class MyComponent : IMyComponent
{
    private readonly IHttpContextAccessor _contextAccessor;

    public MyComponent(IHttpContextAccessor contextAccessor)
    {
        _contextAccessor = contextAccessor;
    }

    public string GetDataFromSession()
    {
        return _contextAccessor.HttpContext.Session.GetString(*KEY*);
    }
}

【问题讨论】:

  • 请提供minimal reproducible example*KEY* 不是有效的 C#。
  • 我收到了上面堆栈溢出问题的代码,猜猜我的问题应该是什么Key
  • 根据您的问题,我认为您不需要GetString()。如果它是您想要的HttpContext,您应该可以使用_contextAccessor.HttpContext 做得很好。该示例仅显示了如何访问存储在 Session 中的字符串。
  • 我建议查看docsKey应该是一个字符串,用于标识您已保存到会话中的项目。 IMyComponentMyComponent 的接口。
  • 是否在依赖注入块中添加了 MyComponent 和 IMyComponent 的设置?

标签: c# asp.net-core .net-core asp.net-core-mvc .net-core-2.0


【解决方案1】:

需要注意的几点:

1.你类继承了一个接口并实现了GetDataFromSession方法。你需要先定义一个接口IMyComponent,如果你想被DI使用,你需要在staryup中注册IMyComponent

public interface IMyComponent
{
    string GetDataFromSession();
}

startup.cs

services.AddSingleton<IMyComponent, MyComponent>();

2.您似乎想从会话中获取数据。 “Key”代表任何会话名称(字符串)。您需要enable session for asp.net core 并首先设置会话值。

_contextAccessor.HttpContext.Session.SetString("Key", "value");

3.在你的创业公司注册IHttpContextAccessor

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

4.完整演示:

MyComponent.cs

public class MyComponent : IMyComponent
{
    private readonly IHttpContextAccessor _contextAccessor;

    public MyComponent(IHttpContextAccessor contextAccessor)
    {
        _contextAccessor = contextAccessor;
    }

    public string GetDataFromSession()
    {

        _contextAccessor.HttpContext.Session.SetString("Key", "value");
        return _contextAccessor.HttpContext.Session.GetString("Key");
    }
}

public interface IMyComponent
{
    string GetDataFromSession();
}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDistributedMemoryCache();

        services.AddSession(options =>
        {
            // Set a short timeout for easy testing.
            options.IdleTimeout = TimeSpan.FromSeconds(10);
            options.Cookie.HttpOnly = true;
            // Make the session cookie essential
            options.Cookie.IsEssential = true;
        });


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddScoped<IMyComponent, MyComponent>();
    }


    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //other middlewares
        app.UseSession();           
        app.UseMvc();
    }
}

API 控制器:

public class ForumsController : ControllerBase
{
    private readonly IMyComponent _myComponent;

    public ForumsController(IMyComponent myComponent)
    { 
        _myComponent = myComponent;
    }
    // GET api/forums
    [HttpGet]
    public ActionResult<string> Get()
    {
        var data = _myComponent.GetDataFromSession();//call method and return "value"
        return data;

    }

【讨论】:

    猜你喜欢
    • 2015-09-23
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多