2017-02-09 6 views
0

Ich habe wenig Problem mit dem Zwischenspeichern meiner Ansichten. Der Standortkopf ist nicht korrekt, wenn ich mein Ticket verloren habe und ausgeloggt bin und versuche, direkt in die URL zu gelangen, in der ich vorher war.ASP .NET MVC OutputCache falscher Standort

Beispiel: Ich bin in/Admin/Kategorien, dann werde ich abgemeldet, weil ich zu lange afk bin, also werde ich zu/Admin/Login weitergeleitet. Nach dem Einloggen versuche ich, zu/Admin/Categories zu gelangen und der Cache schickt mich nach/Admin/Login statt/Admin/Categories.

Mein 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() zurückkehrt wahr oder falsch und es funktioniert gut - es ist nicht das 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 Teil innen:

<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 - Kategorien (/ Admin/Kategorien) - siehe Location-Header was ist falsch ...

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 

Antwort

0

Ok, so Das Problem war, dass der OutputCache-Speicherort mit VaryByCustom, der als Parameter verwendet wird, auf Server oder einen anderen Serverstandort festgelegt werden muss.

Zum Beispiel:

Verwendung in der Steuerung:

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

Diese Lösung arbeitet Alles gut.