【问题标题】:Outputcache IIS with MVC and ASP.NET Core in Azure在 Azure 中使用 MVC 和 ASP.NET Core 的输出缓存 IIS
【发布时间】:2016-04-19 09:34:21
【问题描述】:

我知道 OutputCache 还没有为 ASP.NET Core 做好准备,但我已经阅读了有关 OutputCache 的信息,您可以像这样在 web.config 中配置它:

<configuration> 
     <location path="showStockPrice.asp">     
       <system.webserver>        
         <caching>         
           <profiles>
             <add varybyquerystring="*"location="Any"
               duration="00:00:01" policy="CacheForTimePeriod"            
               extension=".asp">
           </profiles>
         </caching>
       </system.webserver>
     </location>
</configuration>

我可以配置我的 web.config 以将 OutputCache Web.Config 用于 MVC 路由吗?

例如:

http://www.example.com/View/Index/123562

varyByParam 参数为 123562。

谢谢。

【问题讨论】:

    标签: azure iis web-config asp.net-core outputcache


    【解决方案1】:

    您可以使用IMemoryCache 类来存储结果。可以在 here 找到 Microsoft 的示例用法。

    这是一个简单的例子:

    public class HomeController : Controller
    {
        private readonly IMemoryCache _cache;
    
        public HomeController(IMemoryCache cache)
        {
            _cache = cache;
        }
    
        public IActionResult About(string id)
        {
            AboutViewModel viewModel;
    
            if (!_cache.TryGetValue(Request.Path, out result))
            {
                viewModel= new AboutViewModel();
                _cache.Set(Request.Path, viewModel, new MemoryCacheEntryOptions()
                {
                    AbsoluteExpiration = DateTime.Now.AddHours(1)
                });
            }
            return View(viewModel);
        }
    }
    

    【讨论】:

    • 谢谢,但我需要缓存输出,而不是 DTO 或对象;)
    • 你看过ResponseCacheAttribute吗?这至少会在客户端缓存结果。
    猜你喜欢
    • 2020-09-02
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2022-11-17
    • 2015-07-25
    • 2010-09-27
    相关资源
    最近更新 更多