问题
I have an issue with a partial View being cached when it shouldn't be. This partial View is used to display the Logon/Logoff on a page. It uses the simple code below to figure out which link to display
@if(Request.IsAuthenticated) {
<a href="@Url.Action("LogOff", "Account", new { area = "" })">Log Off</a>
}
else {
<a href="@Url.Action("LogOn", "Account", new { area = "" })">Log On</a>
}
This partial View is called from with all pages in my MVC3 application, using
@Html.Partial("_HeaderView")
In most of my controllers, I have the output cache defined, so I can take advantage of caching my content.
[OutputCache(Duration = 86400, VaryByParam = "*")]
Now my issue is that the entire page is being cached when I don't want the partial view to be. This is causing wrong behavior where in it sometimes displays LogOff even if the user is not logged in etc. Is there a way to cache all the content, except for the partial view in question?
回答1:
What you are looking for is called Donut Caching. Here's a great article explaining what it is and how to make it work http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3
回答2:
You can disable caching by decorating the controller which displays your _HeaderView partial with the following:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult HeaderView()
{
return PartialView("_HeaderView");
}
回答3:
We should set cache profile in the Web.config file instead of setting cache values individually in pages to avoid redundant code. We can refer the profile by using the CacheProfile property of the OutputCache attribute. This cache profile will be applied to all pages unless the page/method overrides these settings.
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="CacheProfile" duration="60" varyByParam="*" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>
And if you want to disable the caching from your action which returns partial view [_HeaderView], you can override the config cache settings by decorating that specific action method like shown below:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult RenderPartialView()
{
return PartialView("_HeaderView");
}
Hope this would help you!
回答4:
this working for me..
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
var url = Url.Action("Index", "Web");
HttpResponse.RemoveOutputCacheItem(url);
return RedirectToAction("Index", "Web");
}
回答5:
Took a little while to figure this one out after getting back into MVC. Just put the Cache setting directly in the Partial Header View. As in when displaying the user name. No need for global or server-side code. Only problem is once a page is cached, it will not refresh right away after login. But we keep the speed when needed in the initial browsing for products. Ok trade off in our case.
@if ( Request.IsAuthenticated) { @* when we are authenticated, don't cache any more! *@ HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); HttpContext.Current.Response.Cache.SetNoStore(); HttpContext.Current.Response.Cache.SetNoServerCaching(); @*@Html.Raw(DateTime.Now.ToString())*@ @Html.ActionLink("Welcome " + ( String.IsNullOrEmpty(((System.Security.Claims.ClaimsIdentity)User.Identity).FindFirstValue("UserName")) ? User.Identity.Name : ((System.Security.Claims.ClaimsIdentity)User.Identity).FindFirstValue("UserName")), "Index", "Manage", routeValues: new { Area = "Store" }, htmlAttributes: new { title = "Manage"}) } else { }
来源:https://stackoverflow.com/questions/8786942/disable-caching-on-a-partial-view-in-mvc-3