azure - Azure 中具有 MVC 和 ASP.NET Core 的 Outputcache IIS

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

我知道 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。

谢谢。

最佳答案

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

这是一个简单的例子:

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);
    }
}

关于azure - Azure 中具有 MVC 和 ASP.NET Core 的 Outputcache IIS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36714376/

相关文章:

c# - 无法解析属性 'CosmosDBAttribute.ConnectionStringSetting' 的值

iis-7 - ASP.NET 3.5 IIS7 角色安全实现

php - 在 IIS 上部署 PHP 应用程序

windows - 移动设备模拟器无法访问本地主机

asp.net - 使用 Visual Studio Web 服务器运行时的身份验证对话框

asp.net-mvc - 使用 IIS7.5 将网站的 URL 绑定(bind)到另一个网站 Controller

asp.net - 区分开发、登台和生产环境之间的 web.config

azure - ARM模板: output instances resources name with Copy

azure - 我们可以在 Azure VM 中创建 Azure 服务总线(用于调试工作流和远程事件接收器)吗

azure - Imagemagick 无法从 tif 转换为 jpg