Asp.Net output Caching not working - asp.net

I'm trying to cache some of my ASP.Net pages.
I followed this tutorial and managed to make it work properly : http://msdn.microsoft.com/en-us/library/sfw2210t%28v=VS.90%29.aspx.
Now, when applying the exact same technic on my ASP.Net website, I have absolutely no cache whatsoever... When hitting F5 on my page, it simply reloads the same page again and again...
I added this on top of my aspx page :
<%# OutputCache Duration="15" VaryByParam="none"%>
And then on my Load method, basically writing the current time to check if it's updated or not, just like the above example... Unfortunately, the time changes everytime I hit F5...
What am I missing here ? Some configuration or ?
Thanks for your help !

I've found that our project used a framework that called the following code :
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
That was the source of my issues...

Related

How make Firefox see changes to .aspx page?

I created an aspx page and viewed it in Firefox and it worked correctly, running the code. But when I make changes to the page (including deleting everything and serving up a blank page), Firefox continues to show the original compiled aspx page! How can I get it to see the new page?
I even added the following code, but it still loads the original page:
<script runat="server">
Sub Page_Load
Random rd = new Random();
Response.AddHeader("ETag", rd.Next(1111111, 9999999).ToString());
Response.AddHeader("Pragma", "no-cache");
Response.CacheControl = "no-cache";
Response.Cache.SetNoStore();
Response.Expires = -1;
End Sub
</script>
I even cleared Firefox's cache, but it still loads the original version!
EDIT: It appears the issue might be on the ASP.Net side. It's also not changing in Chrome. So, how do I force changes to an aspx file to force a recompiling?
I've had luck with the following code:
// Stop Caching in IE
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
// Stop Caching in Firefox
Response.Cache.SetNoStore();
Also, your browser may have already cached it prior to when you added the cache prevention code. If so, try hitting CTRL-SHIFT-R in Firefox to force a reload without hitting the cache and see if you still have a problem with Firefox storing cached copies of your page.
Since it appears the issue is not firefox related but something in the ASP.Net web server (since it happens in Chrome as well), I've asked a different question and will close this one.
Yet another way to reload and override cache CTRL + F5
It may also depend on how you are developing your ASP.Net application/site
re-compiling after making changes before viewing again?
If you are building a web application, you have to rebuild (In Visual Studio) your application to reflect any change(s) you made to server side code - this isn't an "ASP.Net issue" it's the norm. You can make changes to the HTML (client side) without re-building, but any server code change will need a rebuild (recompile the dll(s)).
The sample code you have above however, looks like in-line code - is all your code in-line - as in you don't have code behind files (i.e. foo.aspx.vb or foo.aspx.cs)?
All the above is based on local development - obviously if you are viewing your "production"/live site, then it goes without saying that you have to "push"/"publish"/upload/update, whatever local changes you made to it.

Failed to load viewstate error after moving website to a new server

I don't really know where to start with this one. I am getting:
`Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.`
after moving a website to a new server. The exact same code works on my other server. It happens when I submit one of my forms (but doesn't do it on all form submissions).
Any ideas what can cause this so I have somewhere to look?
Using: ASP.NET 2.
EDIT: I am adding some user controls to a placeholder dynamically at runtime but this same code is working ok on my other server. I have tried clearing the controls in the place holder before adding new ones (as I saw a post about that) but it hasn't helped.
EDIT2: It seems that the postback is just failing. It isn't going into the onClick code of the button either so something is deffintiely screwy .. If I try / catch the exception it seems that all the controls are still added successfully ... Setting my Dynamic UC's to EnableViewState = false resolves this particular error.
EDIT3: Ok, I think I may have a handle on what is happening. For some reason on the old server the form action is default.aspx?action=amend but the new server is showing amend.html?action=amend so I think the re-write module is messing up in IIS. This would explain the control adding issue as well because the action is happening 2 times (I think). I will look into the Rewrite module and see if anything is wrong then post back.
Please, have a look at these articles:
http://blog.typps.com/2008/01/failed-to-load-viewstate-typical.html
http://weblogs.asp.net/guys/archive/2004/12/05/275321.aspx
Or try a simple temporary solution - disable viewstate for this placeholder. Either way, I'm puzzled why it actually works on your first server. I'd be glad if someone else will be able to clarify this subject more.
It turns out that the post back Url for the form is wrong on this server (unsure why at the moment, I will update when I know). This is causing the dynamic controls to be added in an unexpected way and causing the error. I noticed this when I managed to post my form and the content didn't update. I manually adjusted the action url using firebug and all is well.
Worth looking at walther's answer regarding dynamic controls and the viewstate though.
Not sure what caused it but I am manually setting the form action in the page load now and it seems to have solved the issue.

ASP.Net Enabling Trace in Code Behind

I'm looking for a way to enable trace for an ASP.Net page via codebehind without using the page or webconfig methods.
My end goal is to have a page that reacts to a querystring element such as ?trace=true then show the tracing information, but prior to that the page would display normally.
I've done some googling and my combination of words ends up with tutorials on using trace in general so any help would be much appreciated.
Thanks
-Birk
You can set Trace.IsEnabled to true in the codebehind.
You could add an iframe to your page that refers to trace.axd and only render this iframe if the query string contains "trace=true"

RegisterClientScriptResource + AJAX update panel

I have a problem that is really making me feel dumb. I have a custom control that inherits textbox. This textbox (at least for this example) simply has a .js file that is embedded in the .dll.
Long story short, works great when not in an AJAX update panel. So i did some research and found that instead of using: Page.ClientScript.RegisterClientScriptResource, i should use ScriptManger.RegisterClientScriptResource - i have done this in the overrdive render method, but the javascript still does not fire.
Anyone know what im doing wrong?
example:
i have a folder in my project called Scripts - it contains myscript.js
My Assembly is called Jim.Bob.Controls
I add attributes to my controls ie: Attributes.Add("onclick", "Test2();");
In the Override Render:
ScriptManager.RegisterClientScriptResource(this.Page, typeof(CustomTextBox), "Jim.Bob.Controls.Scripts.myscript.js");
Yet i still get 'object expected' error.
I need my textbox to work with and w/o AJAX. I imported System.Web.Extensions into my project to access ScriptManger
Can someone please tell me what im doing wrong? Again, this whole thing worked fine w/o AJAX, i have put the necessary stuff into the AssemblyInfo.cs (WebResource:,,,,etc)
Thanks in advance :-)
--- UPDATE ---
I reverted the control, trying it in a non AJAX web and i am having the same problem. Not sure why i have a problem, i have another custom control in the same assembly that is working just fine - have them setup the exact same way, only difference is the one that is working inherits WebControl, the one that is not inherits TextBox
...
in the one that is working i emmit html like Go and do it
Where the one that is NOT working i have
Attributes.Add("onclick", "CustomFunction();");
Also, if i do Attributes.Add("onclick", "alert('hello');");
it works fine.
Sorry for such a long post.
Try to pass this instead of this.Page. The ScriptManager would output scripts only for controls which are being updated (children of UpdatePanel that is).
I've just noticed that you are doing this during Render. That's too late. Try PreRender instead.

Error appeared when I changed default page in visual studio

First, I did not like the index.cshtml page is that it contains the signature of Web API ASP.net, the API link, ...so on. And it also it contains the footer as '2015 # My Asp.net Application'.
So, is there a way that I can get rid of the header and signature? I found the solution as that could be at the master page, but I couldn't find the master page with extension (.master) in my application.
Please help me in getting rid of the header and signature of Asp.net Web API.
Second, I set another page as 'Set page as Start page' or as default page. Then when I changed my mind and came back and made the index.cshtml page as default page as it was, I got Server Error (page not found).
Please help me in either one way where I can create a new html page to add my code in it, and I can make it as start page without problems.
Or at least, let me fix the error that occurred in the index.cshtml page, where I can come back to it and use it again.
Thank you
In MVC razor the master page is referred to as the _Layout.cshtml page it can be found inside Views->Shared.
To set the default page you would have to set the default action and controller in the routing table
Footer
Ctrl+Shift+F Search Entire Solution for: "My Asp.net Application". This displays _Layout.cshtml as a search result.
Default Page
Go To Project > Properties > Web.
Remove Start Action Specific Page (make it blank).

Resources