I'm using OutputCache for caching an horizontal menu and a vertical menu in my app. I usually use something like this in the actions I want to be cached
[OutputCache(Duration=3600, VaryByParam="none", Location=OutputCacheLocation.Client, NoStore=true)]
public ActionResult ActionName()
{
.......
}
But if it is a child actions i must use
[ChildActionOnly]
[OutputCache(Duration = 180, VaryByParam = "none")]
public ActionResult Menu()
{
......
}
When you use OutputCache with child actions you can´t specify properties like Location or NoStore. So the question is, if i can´t specify the cache location (client, server, any) for a child action, where is it stored by default?? Thanks!!
(sorry for my english)
I'm just guessing here, but it would probably get stored just on the server. The idea of a partial view (likely the result of a child action) being stored on the client doesn't make sense -- the client doesn't have any idea of the page's action break-down on the server.
The way I see it, unless the entire page is cached, the client must go to the server to get the page rendered, at which point, the server can returned the cache child action result.
When we use Output Cache for child action it is cached on Server not on client side.
Unfortunatly it is cached on client, Just set a breakpoint on your childAction Method, and run application from multiple browsers, for each browser ChildAction will called in cache duration.
Related
I have a site whose layout contains some partial views. One for the page header, other for a menu that is shown to the left, and other for a page footer.
The body is not a partial view. Just a #RenderBody() call.
I need to increase page loading so I tried to use DevTrends.MvcDonutCaching.DonutOutputCache attribute.
The problem I have is that the page header shows information about the current session that changes according which option the user chose at log in time.
Look at one Index action in a controller:
[DevTrends.MvcDonutCaching.DonutOutputCache(Duration = int.MaxValue, VaryByParam = "none", Location = System.Web.UI.OutputCacheLocation.Client)]
public async Task<ActionResult> Index()
{
return View();
}
Well... with that, the client browser will store the whole page, including, of course, the header.
The bad thing with this is that if the user logs out and logs in again chosing other option, the header shows the options chosen in the previous session, unless user presses Ctrl-F5 to reload the page clearing the cache.
I had set the cache location in the server at first, but that was the worst. The same header appears for all uses that log in to the system. That is why I changed location to client.
When user logs out the session, I am running this code:
var cacheManager = new DevTrends.MvcDonutCaching.OutputCacheManager();
cacheManager.RemoveItems();
That did not work either.
I was thinking to stop using caching at all, trying to delete cache using some Javascript function or searching for a way to not store in cache the page header.
What is your advice with this?
Thanks
Jaime
I'm working on a site that has routes to actions which render Partial Views. A lot of these partial views are components which together make up a complete page.
For instance on a search page I'm working on has a text box, a list of tabs, and a Table.
Seach of these can be accessed with a URL similar to
/Search/SearchPanel
/Search/Tabs/{SearchTerm}
/Search/ResultsTable/SearchTerm?tab=[currently selected tab]
and these are all rendered on with a RenderPartial on my Index page.
When the page loads, it will display each of these components the way I want it. But at the moment there's nothing stopping a user from going directly to the url
/Search/Tabs
to render only a tab control which is meaningless outside the context of the rest of the elements on the page.
Is there a way for me to prevent this?
Have you tried marking your Controller method as private?
private PartialViewResult MyPartialResultMethod()
This should allow you to call it from within your code to build up your pages and disallow any public access such as through a URl.
I'm testing this now to make doubly sure my answer is correct so I'll update as I test.
In your tabs example you could simply restrict access by using a second controller method for Tabs that's private.
So you'd have something that looks like:
public ActionResult Tabs(string searchTerm) // When a search term is passed.
and
private ActionResult Tabs() // When no search term is passed.
You could create an ActionFilter which checks if the Request.IsAjaxRequest() is true. If it's not (meaning the user is calling the view directly), re-direct accordingly.
I have a value which I want to be vaild during a single request. I am not using Session, as this would make the value global for the entire navigation session.
So I have put thie value in a static field of a class. Great, but then I discovered that such fields are even more global, that is, they stay set for the entire application! This means that there could be random interaction among navigation sessions.
So the question is: is there a safe place I can put a global variable, which will be
global throughout the request
reset after the request is completed
not affected by any other request, either of the same user or by other users
Thanks
Palantir
EDIT
I'll elaborate. I have a piece of code in my master page, which I need to hide on certain conditions, of which I am aware in the controller only. I thought about setting a static variable in the controller, which then would be queried by the master page, but now I see there could be a better way...
Use HttpContext.Items - a per-request cache store. Check out this article on 4guysfromrolla for more details.
It should work fine in ASP.NET MVC. You may wish to derive your master page from a base class (either via code-behind or using the Inherits directive) and have a protected method on the base class that inspects HttpContext.Items and returns, e.g. true/false depending whether you want to display the conditional code.
TempData lasts until the next request as already noted.
But there are also two other dictionaries scoped to the single request.
{Controller,ViewPage}.ViewData
Context.Items
To communicate from controller to (master) page ViewData is probably the better choice.
Two approaches come to mind:
Create a base controller where you set this variable, and then have all your controllers inherit from that.
Use TempData - the problem here being that it sticks around for the next request. But maybe knowing that, you can work around it by using a GUID key to determine that you are, in fact, getting a new value when you need it.
I would probably go with 1).
The common way to access data in a MasterPage that is set in Controller (Action) is via ViewData["TheDataKey"] = "SomeValue".
This is relatively easy and there are a couple of ways that you can do it - depending on how your site works.
I'm interpreting your request as that you want a property or variable that exists for the duration of the request and is visible to the controller, model and master.
A static property is visible to the current application in ASP this means a load of users connecting at once, but not necessarily all of them. IIS will spawn new ASP applications as it needs to.
So the ways you can do this:
You can have a custom base class for your master page or a code-behind page (as all the WebForms stuff still works)
You can have a custom base class for your controllers.
You can get to one from the other, so:
void Page_Init( object sender, EventArgs e )
{
var ctrl = this.ViewContext.Controller as MyBaseController;
if ( ctrl != null )
{
MyLocalProp = ctrl.PropOnMyController;
}
}
This will then be available in the controller and the master page on a per Request basis.
Did you look into the tempData that is attached to the controller class? it is a simple dictionary that preserves it's value through one single request.That would meant that your data can only be accessed in the controller but that should not be a problem.
public class MyController : Controller
{
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult MyAction(string id)
{
this.TempData["Message"] = "YourData";
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MyAction(string Id)
{
var myData = this.TempData["Message"];
}
}
This works for me. I use it only to display warning messages and stuff like that.
Since upgrading from mvc 2 beta 2 to rc I'm having trouble with an ajax submission in Internet Explorer. Upon carrying out a jquery form post, the function returns a url to a controller action. This worked fine with the controller action picking up it was a ajaxrequest and then piping back a partial view to update the page. This still works fine in Firefox, however in Internet Explorer the final call to the controller action now comes from cache and returns therefore returns a full view rather than partial.
I've tried setting the outputcache to 0 with no success and I've also tried the nocache actionfilter as described here Disable browser cache for entire ASP.NET website with no luck. The only way I can stop IE from pulling from cache is to physically delete the cached version.
Anyone have any ideas (apologies if this isn't very clear, tricky one to explain!)?
For some reason, IE is really aggressive about caching AJAX GETs. So if you are fetching that via AJAX, the behavior is not surprising to me. Also not surprising is that using output cache attribute didn't fix the problem, because it is IE, rather than the server, which is doing the caching. What you need to do is to tell IE not to cache the request, by setting the appropriate headers in the HTTP. Here is how we do it:
[CacheControl(HttpCacheability.NoCache), HttpGet]
public JsonResult DoStuff()
{
//...
}
public class CacheControlAttribute : ActionFilterAttribute
{
public CacheControlAttribute(HttpCacheability cacheability)
{
this._cacheability = cacheability;
}
private HttpCacheability _cacheability;
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
cache.SetCacheability(_cacheability);
}
}
My 2 cents to Craig's great solution -
If you're using a user control and rendering it using an "RenderAction"
<% Html.RenderAction("UserList", "User"); %>
And this page invokes it via GET as well as POST then don't mention the request method in the attribute declaration.
[CacheControl(HttpCacheability.NoCache)]
For example, I've a search Grid and a search panel on top of it. I've made my Grid a user-control so that I can invoke the sort & paging via AJAX (GET) still it is also invoked when I press the "search" button (POST) so I need it for both.
I'd like to get access to the current executing Controller so I can offload the return of the appropriate ActionResult onto a helper method. To this end, I'm looking for the equivalent of what I would have thought would be ControllerContext.Current but isn't. Thanks!
Edit for clarification: I've got a generic form control which is JavaScript-based but I'd like to add an option so that it works with noscript. At the moment my Controller sets the ViewData.Model to a JSON-ified Models.FormResponse<T>.
This FormReponse is set up with the status of the post and any error messages that were generated, so I'd like a GetActionResult() method which does the script/noscript check (a hidden form input) and either:
Sets the Model to the JSONed FormResponse and returns a View(), or
Serializes the FormResponse to the Session and returns a Redirect().
As this obviously changes the return value and I don't want to do the check myself every time, I need to call View or Redirect from the FormResponse's GetActionResult method in order to call this as:
return formResponse.GetActionResult();
I know with a more astronautical design this could be made even more robust but as the noscript option is not a major feature at the moment, I just need to get a quick solution working that doesn't break other things.
Update #2
The following, implemented in an ActionResult class, does the job for me. Thanks CVertex!
public override void ExecuteResult(ControllerContext context)
{
if (CMSEnvironment.NoScript)
{
Oracle.Response.Redirect(Oracle.Request.UrlReferrer.ToString(), true);
}
context.Controller.ViewData.Model = _model.ToJSON();
new ViewResult()
{
ViewName = Areas.Site.Helpers.SharedView.Service,
ViewData = context.Controller.ViewData
}.ExecuteResult(context);
}
Statics are bad for testability, and very much discouraged in MVC.
Why do you want to access the current controller and action method?
The best way to do this is to implement your own ActionFilter.
This gives you a means of intercepting requests before or after actions methods execute.
EDIT:
By intercepting the result inside OnActionExecuted of a filter, you can do your noscript/script checks and modify your ViewData accordingly for consumption by the View.
Inside OnActionExecuted, you can also do the noscript check and have complete control over the final ActionResult or the ViewData, as you please.
Or, you can write your own ActionResult that makes all these decisions.
So, your controller action ultimately does
return new MyActionResult(format_and_view_agnostic_model_object);
There doesn't appear to be a way to navigate to the current Controller from a thread. That is you could get the ControllerBuilder and you can get the MvcHttpHandler but neither then lets you access the controller instance that the handler is using.