简体   繁体   English

ASP.net核心ResponseCache和RedirectToAction。 如何缓存匿名只响应?

[英]ASP.net Core ResponseCache and RedirectToAction. How can anonymous only responses be cached?

Given the following controller: 给出以下控制器:

public class MyController : Controller
{
    [AllowAnonymous]
    [ResponseCache(VaryByQueryKeys = new string[] { "id" }]
    public async IActionResult Action1(string id)
    {
        if (User.Identity.IsAuthenticated)
           return RedirectToAction("Action2", new {id = id});

        return View();
    }


    [Authorize]
    public async IActionResult Action2(string id)
    {
        return View();
    }
}

Suppose an authenticated user navigates to "/Mycontroller/Action1/20". 假设经过身份验证的用户导航到“/ Mycontroller / Action1 / 20”。 Would the response be cached? 是否会缓存响应?

If the answer is yes, how can anonymous only responses be cached? 如果答案是肯定的,那么如何只缓存匿名响应?

In the code you're showing here, the answer is no. 在你在这里展示的代码中,答案是否定的。 The RedirectToAction will issue a separate request (a 301 redirect) to your site, which will be handled completely independently from the first request (to Action1). RedirectToAction将向您的站点发出单独的请求(301重定向),该请求将完全独立于第一个请求(至Action1)进行处理。

It is worth considering the effects of response caching on non-anonymous users, but in this case your scenario would be safe from these potential effects. 值得考虑响应缓存对非匿名用户的影响,但在这种情况下,您的场景可以安全地避免这些潜在影响。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM