I have little problem with caching my views. The location header isn't correct when I lost my ticket and get logged out, and trying to get directly into url I was before.
Example: I'm inside /Admin/Categories, then I'm getting logged out due to being afk too long, so I'm being redirected to /Admin/Login. After log in I'm trying to go to /Admin/Categories and cache is sending me into /Admin/Login instead of /Admin/Categories.
My code:
LOGIN CONTROLLER
[OutputCache(CacheProfile = "OneDayCache", VaryByParam = "None", VaryByCustom = "url")]
public ActionResult Index()
{
return View();
}
CATEGORIES CONTROLLER
[OutputCache(CacheProfile = "OneDayCache", VaryByParam = "None", VaryByCustom = "url")]
public ActionResult Index()
{
if (validations.ValidateTicket())
{
return View();
}
else
{
return RedirectToAction("Index", "Login");
}
}
validations.ValidateTicket() is returning true or false and it's working good - it's not the problem.
GLOBAL.ASAX.CS
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg == "url")
{
return context.Request.RawUrl;
}
return base.GetVaryByCustomString(context, arg);
}
Web.config part inside :
<caching>
<outputCache enableOutputCache="true" omitVaryStar="true"></outputCache>
<outputCacheSettings>
<outputCacheProfiles>
<add name="OneDayCache" duration="86400" location="Client" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
Cache - Login (/Admin/Login)
HTTP/1.1 200 OK
Cache-Control: private, max-age=86400
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Expires: Fri, 10 Feb 2017 20:35:45 GMT
Last-Modified: Thu, 09 Feb 2017 20:35:45 GMT
Vary: Accept-Encoding
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.2
X-Frame-Options: SAMEORIGIN
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?TTpcUHJvamVrdHlcQU1CSVQtQ01TLU1WQ1xBTUJJVCBDTVMgTVZDXEFkbWluXExvZ2lu?=
X-Powered-By: ASP.NET
Date: Thu, 09 Feb 2017 20:35:45 GMT
Content-Length: 1113
Cache - Categories (/Admin/Categories) - look at location header which is wrong...
HTTP/1.1 302 Found
Cache-Control: private, max-age=86400
Content-Type: text/html; charset=utf-8
Expires: Fri, 10 Feb 2017 20:35:39 GMT
Last-Modified: Thu, 09 Feb 2017 20:35:39 GMT
Location: /Admin/Login
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?TTpcUHJvamVrdHlcQU1CSVQtQ01TLU1WQ1xBTUJJVCBDTVMgTVZDXEFkbWluXENhdGVnb3JpZXM=?=
X-Powered-By: ASP.NET
Date: Thu, 09 Feb 2017 20:35:39 GMT
Content-Length: 439
Ok, so the problem was that OutputCache location with VaryByCustom used as parameter needs to be set to Server or any other using Server location too.
For example:
Usage in controller:
[OutputCache(CacheProfile = "ControllerIndexCache")]
Web.config:
<caching>
<outputCache enableOutputCache="true" omitVaryStar="true"></outputCache>
<outputCacheSettings>
<outputCacheProfiles>
<add name="ControllerIndexCache" duration="10" location="Server" varyByCustom="Url" varyByParam="None" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
Global.asax.cs:
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg == "Url")
{
return context.Request.Url.AbsoluteUri;
}
return base.GetVaryByCustomString(context, arg);
}
This solution is working just fine.