C# 缓存Asp.Net不';不存在Asp.NET5

C# 缓存Asp.Net不';不存在Asp.NET5,c#,asp.net-mvc,caching,asp.net-core,asp.net-core-mvc,C#,Asp.net Mvc,Caching,Asp.net Core,Asp.net Core Mvc,我正在使用Asp.net 5和MVC 6,目标是.net framework 4.5.2 我想使用以下代码: Cache["test"] = "test"; 或 但两者都会得到以下错误,即缓存在此上下文中不存在。 我错过了什么 编辑: 如下所述,您可以通过将IMemoryCache接口注入控制器来使用它进行缓存。这在asp.net 5 RC1中似乎是新的。更新您的startup.cs以将其包含在配置服务中: services.AddCaching(); 然后更新控制器,使其具有IMemory

我正在使用Asp.net 5和MVC 6,目标是.net framework 4.5.2 我想使用以下代码:

Cache["test"] = "test";

但两者都会得到以下错误,即缓存在此上下文中不存在。 我错过了什么

编辑:


如下所述,您可以通过将IMemoryCache接口注入控制器来使用它进行缓存。这在asp.net 5 RC1中似乎是新的。

更新您的
startup.cs
以将其包含在
配置服务中:

services.AddCaching();
然后更新控制器,使其具有
IMemoryCache
的依赖项:

public class HomeController : Controller
{
    private IMemoryCache cache;

    public HomeController(IMemoryCache cache)
    {
        this.cache = cache;
    }
然后你可以在你的行动中使用它,比如:

    public IActionResult Index()
    {
        // Set Cache
        var myList = new List<string>();
        myList.Add("lorem");
        this.cache.Set("MyKey", myList, new MemoryCacheEntryOptions());
        return View();
    }

现在,在MVC6中,您可以通过将IMemoryCache接口注入控制器,使用IMemoryCache接口进行缓存

using Microsoft.Extensions.Caching.Memory;

public class HomeController
{
    private readonly IMemoryCache _cache;

    public HomeController(IMemoryCache cache)
    {
        if (cache == null)
            throw new ArgumentNullException("cache");
        _cache = cache;
    }

    public IActionResult Index()
    {
        // Get an item from the cache
        string key = "test";
        object value;
        if (_cache.TryGetValue(key, out value))
        {
            // Reload the value here from wherever
            // you need to get it from
            value = "test";

            _cache.Set(key, value);
        }

        // Do something with the value

        return View();
    }
}

可能重复访问缓存的位置?我无法复制它
public ActionResult Index(){HttpContext.Cache[“Name”]=123;var value=HttpContext.Cache[“Name”];return View();}
您能创建一个新的ASP.Net项目并重试吗?是的,我创建了一个新项目,它仍然说HttpContext不包含缓存的定义,这很奇怪,我必须添加一些包或指令吗?请尝试
HttpRuntime.Cache
    public IActionResult About()
    {
        ViewData["Message"] = "Your application description page.";

        // Read cache
        var myList= this.cache.Get("MyKey");

        // Use value

        return View();
    }
using Microsoft.Extensions.Caching.Memory;

public class HomeController
{
    private readonly IMemoryCache _cache;

    public HomeController(IMemoryCache cache)
    {
        if (cache == null)
            throw new ArgumentNullException("cache");
        _cache = cache;
    }

    public IActionResult Index()
    {
        // Get an item from the cache
        string key = "test";
        object value;
        if (_cache.TryGetValue(key, out value))
        {
            // Reload the value here from wherever
            // you need to get it from
            value = "test";

            _cache.Set(key, value);
        }

        // Do something with the value

        return View();
    }
}