Substitution Control and Cache Location - asp.net

If I use Substitution control in asp.net page, and also add the following directive to the page:
<%# OutputCache Duration="7200" VaryByParam="None" Location="Any" %>
Would the location attribute be ignored, since using Substitution control on a page makes the page cacheable only on the server?

Yes, it will be ignored. Substition.Render calls, through RenderMarkup, HttpResponse.WriteSubstitution that calls HttpCachePolicy.SetCacheability(HttpCacheability.Server).

Related

How to use: HttpResponse.RemoveOutputCacheItem when using varyByCustom

I have implemented the Redis OutputCache (Microsoft.Web.RedisOutputCacheProvider) using the Azure Redis service.
I have found similar unanswered / unsolved questions here and here.
On top of the pages need to cache:
<%# OutputCache VaryByParam="*" Duration="600" %>
After access a random page and I check the key in Redis database is has saved like: /_a2/monitor.aspx
So running the below line of code, will remove this cache item (and it worked).
HttpResponse.RemoveOutputCacheItem("/monitor.aspx", "RedisOutputCache")
Now I have updated the same page adding the varyByCustom the the OutputCache directive:
<%# OutputCache VaryByParam="*" varyByCustom="userhash" Duration="600" %>
Now accessing the page and checking the key in Redis database it is saved like: /_a2/monitor.aspxHQFCNuserhashVcc6ef5b7173286704cef942d5577b88bd81f2cce71a0dcdc8676d3a815e68b59DE
You see the added userhash hash value, this is nice and worked as expected.
But now here comes the problem: How do I clear this cache item.
This is not working:
`HttpResponse.RemoveOutputCacheItem("/monitor.aspx", "RedisOutputCache")`
Also tried:
HttpResponse.RemoveOutputCacheItem("/monitor.aspx?userhash=cc6ef5b7173286704cef942d5577b88bd81f2cce71a0dcdc8676d3a815e68b59", "RedisOutputCache")
And also tried:
HttpResponse.RemoveOutputCacheItem("/monitor.aspx {userhash:cc6ef5b7173286704cef942d5577b88bd81f2cce71a0dcdc8676d3a815e68b59}", "RedisOutputCache")
Also tried using some of the below options in combination of the above code:
Response.Cache.SetVaryByCustom("userhash")
Response.AddCacheItemDependency("action")
HttpContext.Current.Cache.Item("action") = "test"
Response.Cache.VaryByParams("userhash") = True
HttpResponse.RemoveOutputCacheItem("/monitor.aspx")
Is there an way to remove this cache item on using the varyByCustom option?
I don't use the system.web.mvc object. So I can not access this object for URL helper.
Any help is most welcome!
Joël
According to your description, I tried to implement my OutputCacheProvider to store cache into files to test this issue on my side. Here is my test result, you could refer to it.
OutputCache of my Monitor.aspx
<%# OutputCache VaryByParam="None" Duration="60" Location="Server" %>
When accessing the above page, I would get the following log from my OutputFileCacheProvider as follows:
And the current DateTime string would be refreshed in my monitor.aspx page after I have accessed another .aspx to clear the cache.
<%# OutputCache VaryByParam="*" Duration="60" VaryByCustom="userhash" Location="Server" %>
Note: The same way to call HttpResponse.RemoveOutputCacheItem("/monitor.aspx") for cleaning cache from another .aspx endpoint.
Both VaryByParam and varyByCustom could work as expected on my side. I assumed that you could add log within your OutputCacheProvider and try to find whether you have missed something.

What is the Page.ResponseEncoding default value?

I knew that what is the purpose of the ResponseEncoding.
And I knew I can set it in the Page directive like below.
<%# Page Language="C#" ResponseEncoding="UTF-8" %>
But What is the default value of it when it is ignored in the Page directive? thanks.
I dont think there is any default value.
The MSDN says:
Remarks:
In most circumstances, do not set this property in code. Set the
ResponseEncoding attribute to the value you want using the # Page
directive in the .aspx file. When the page is requested, the
dynamically generated class sets the property.

How to use outputcache in a usercontrol, with control properties

I have a UserControl, which should only change based on 2 URL parameters.
The problem is, it has a public property, which is used in the calling pages, so it throws a NullReferenceException on my property.
Any ideas?
I think I've figured this one out, it seems to be quite tricky which is due to my lack of comprehensive understanding of how output cache works I suspect.
You can't cache the UserControl if it has variable properties that dicatate it's content. You need to put a cache control in the Content page that holds the control. Then add the cache to the content page:
<%# OutputCache Duration="120" VaryByControl="JobList" %>
Where the vary by control is the ID of the control you wish to cache. Then specify a property for that vary by control:
<%# OutputCache Duration="120" VaryByControl="JobList.LoggedInUserID" %>
This seems to work for me!
Check the VaryBy options,
try to take a look to this articles:
http://msdn.microsoft.com/en-us/library/hdxfb6cy%28v=vs.71%29.aspx
https://web.archive.org/web/20211020113508/https://www.4guysfromrolla.com/articles/022802-1.aspx
http://weblogs.asp.net/stefansedich/archive/2008/03/17/output-cache-with-usercontrol-on-masterpage-and-multiple-varybycustom.aspx

Asp.net jquery post to ascx: This type of page is not served

When I'm trying to call an ascx with Jquery post I get:
"This type of page is not served"
I think it's something to do with the IIS not allowing calls directly to ascx.
Is it possible to allow posting to ascx?
I have IIS6.
Thanks.
No, .ascx is a control, it is included and rendered in .aspx pages.
So the error is correct, ascx should not be served.
Wrap it in an aspx page like so:
<%# Page %>
<%# Register TagPrefix="scott" TagName="header" Src="Controls/Header.ascx" %>
<scott:Header runat=server id="control1" />
Just FYI, in an MVC application, you are actually allowed to load an .ascx by pointing the URL to an Action that returns a PartialView.

Why is #MasterType directive not implied when setting #Page masterPageFIle?

The docs for #MasterType have this example.
<%# Page masterPageFile="~/MasterPage.master"%>
<%# MasterType virtualPath="~/MasterPage.master"%>
Why is #MasterType even needed? Couldn't the compiler automatically take the same actions based solely on #Page masterPageFile? When would you not want to use both?
You can set different master pages at each pages PreInit. So the master page is in general not well defined.

Resources