Reputation: 558
I need to force to output cache a specific child action on a user's login because I have to avoid caching this child action by a parent action which uses the AuthorizeAttribute
.
However when calling the child action programmatically inside the Login action method to cache it, the caching doesn't work.
Child action call:
HomeController homeController = new HomeController();
var actionResult = homeController.MainMenu();
Child action:
[OutputCacheAttribute(Duration = 300, VaryByCustom = "User")]
[ChildActionOnly]
public ActionResult MainMenu()
{
return PartialView("MainMenu");
}
Addition:
I am aware of the issue that this procedure contravenes against the concept of the caching process. However I need to do this because the login can redirect the user to its logout page where the child action can be called by a action which uses the AuthorizeAttribute
. The child action is part of the layout page, therefore I need it to be prerendered/cached by action method which doesn't uses the Authorize Attribute. This is because the AuthorizeAttribute
doesn't support output caching of child actions.
Upvotes: 1
Views: 2912
Reputation: 68
Child actions are the actions called inside views with Html.Action() helper.
You should call your child action inside your login view not inside your controllers action.
I believe output caching should work then.
You should read up on Donut and Donut Hole caching http://www.dotnet-tricks.com/Tutorial/mvc/ODJa210113-Donut-Caching-and-Donut-Hole-Caching-with-Asp.Net-MVC-4.html
I believe that's what you're trying to achieve here.
Upvotes: 2
Reputation: 239250
The output cache is handled by the request processing machinery. Calling the method directly doesn't activate any of that. I'm not sure what you're trying to achieve here, but it seems this is pointless anyways. The first time the child action is rendered, it will be cached for the user. Priming the cache on login only moves that few milliseconds of processing time from one request to the other. Either way, the user is subjected to the initial load of the child action, it's only a case of when. Given that, just let nature take its course, and let it happen when it should: the first time it's needed.
Upvotes: 0