how to cache a control, js files and css files in .net? - asp.net

I have a master page, inside that I have a menu controls and other css, JS files.
Whenever I refresh the page from client, everything is coming from the server.
Now I want to cache those controls and those files as per session.
Is there any setting or programmatic settings there to do it?

You should read up on ASP.Net Caching - there are lots of flexible ways to cache your pages.
However in your case, it sounds like you should just be able to add the OutputCache directive to the top of your page:
<%# OutputCache Duration="60" VaryByParam="None"%>
Static CSS and JS files should be cached automatically by the browser. If you've embedded them as webresources then the caching directives will help you cache them as well.

There's no problem with JS and other items cached on client.
But yes, there's a concern on dynamic controls as they are not simply compiled and depend on request. And here's an article in Smöråkning blog on how to cache them.

Related

Loading src files once per session in asp.net

I have way too many pages in the application that basically load the same set of xml and js files for client side interaction and validation. So, I have about dozen lines like this one <script type="text/javascript" src="JS/CreateMR.js"></script> or like this one <xml id="DefaultDataIslands" src="../XMLData/DataIslands.xml">.
These same files are included in every page and as such browser sends request to read them every time. It takes about 900ms just to load these files.
I am trying to find a way to load them on just the login page, and then use that temp file as source. Is it possible to do so? If yes, how and where should I start?
P.S. A link to a tutorial will work too, as I have currently no knowledge about that.
Edit:
I can't cache the whole page, because the pages are generated at runtime based on the different possible view modes. I can only cache the js and xml file. Caching everything might be a problem.
Anyway, I am reading through the articles suggested to figure out how to do it. So, I may not be able to accept any answer right away, while I finish reading and try to implement it in one page.
Edit:
Turns out caching is already enabled, it is just that my server is acting crazy. Check the screenshot below.
With Cache
Without cache
As you see, with cache, it is actually taking more time to process some of the requests. I have no idea what that problem is, but I guess I should go to the server stack exchange to figure this out.
As for the actual problem, turns out I don't have to do anything to enable caching of xml and js files. Had no idea browsers automatically cache js files without using specific tag.
Totally possible and in fact recommended.
Browsers cache content that have been sent down with appropriate HTTP caching headers and will not request it again until the cache has expired. This will make your pages faster and more responsive and your server's load much lighter.
Here is a good read to get you started.
Here is ASP.NET MVC caching guide. It focuses on caching content returned from controllers.
Here is a read about caching static content on IIS with ASP.NET MVC.
Basically, you want to use browser caching mechanism to cache the src files after the first request.
If you're using F12 tools in your browser to debug network requests, make sure you have disable cache option unchecked. Otherwise, it forces browser to ignore cached files.
Make sure your server sends and respects cache headers - it should return HTTP status 304 Unmodified after first request to a static file.
Take a look at Asp.Net Bundling and minification - if you have for example multiple js source files, you could bundle them into one file that will be cached on the first request.
Additionally, if you use external js libraries, you could download them from a CDN instead of your server - this will both offload your server and enable user browser to use cached script version (meaning - if some other page that user has visited also used the same script, browser should already have it cached).
One approach is caching static files via IIS by adding <clientCache> element in web.config file. The <clientCache> element of the <staticContent> element specifies cache-related HTTP headers that IIS and later sends to Web clients, which control how Web clients and proxy servers will cache the content that IIS and later returns.
How to configure static content cache per folder and extension in IIS7?
Client Cache
for more info on client side caching read this part of Ultra-Fast ASP.NET 4.5 book:
Browser Cache and Caching Static Content
Other approach is caching portions of page.
if your are using Web Form:
Caching Portions of an ASP.NET Page
and if you are using MVC, use Donut Hole Caching
ASP.NET MVC Extensible Donut Caching
Donut Caching and Donut Hole Caching with Asp.Net MVC
The browser has to ask the server if the file has been modified or not since it put it to the cache, therefore the http statuscode 304. Read more from https://httpstatuses.com/304.
As this is asp.net please make sure you are first running it with
<compilation debug="false"/>
as enabling debugging has some side effects which include.
"All client-javascript libraries and static images that are deployed via
WebResources.axd will be continually downloaded by clients on each page
view request and not cached locally within the browser."
More read from https://blogs.msdn.microsoft.com/prashant_upadhyay/2011/07/14/why-debugfalse-in-asp-net-applications-in-production-environment/

ASP.Net pages - cache only images

I am working on applying caching to a set of ASP.Net pages. My question is, can I apply caching only to the images on these pages? If so, how can I do this? I would like to do this without touching the web.config if possible. I have a masterpage that is used on all of the pages that I need to cache.
I was trying to use this, but I couldn't find a way to cache only image files.
Any help is appreciated.
You can configure the HTTP Expires Response header in IIS to cache static content for your web pages.
http://technet.microsoft.com/en-us/library/cc770661%28v=ws.10%29.aspx

Asp.Net OuputCache does effect JavaScript?

I use Asp.net 4 c# and Page OutPut Cache IN MEMORY for optimizing performances.
For my understanding all the HTML generated by Server Controls will be cached for my page (in memory).
If in my Web Page I use references to CSS and JAVASCRIPT Files, example Google Adsense or others.
How Page OutPut Cache behaves?
Even my JAVASCRIPT will be cached?
Thanks for your time.
Page level, or output caching, caches the HTML output of dynamic requests to ASP.NET Web pages
With output caching, the final rendered HTML of the page is cached. When the same page is requested again, the control objects are not created, the page life cycle doesn't start, and none of your code executes. Instead, the cached HTML is served. Clearly, output caching gets the theoretical maximum performance increase, because all the overhead of your code is sidestepped.

How HTML5 offline cache works with ASP.NET?

There are MasterPage.master in my ASP.NET application
My manifest file dose not work:
CACHE
MANIFEST CACHE:
MasterPage.master
Please, tell how should I correctly write MasterPage in my manifest file?
You shouldn't try and cache your Masterpage. If you want to cache any pages that the Master Page uses, instead add the manifest attribute to the HTML tag on your Master Page. That way the HTML page will be cached.
If you need more information on this, I have written an article about MVC and the Application Cache - it should give you a pretty good idea of what you are trying to achieve.
http://deanhume.com/Home/BlogPost/mvc-and-the-html5-application-cache/59
Similarly, try http://diveintohtml5.ep.io/offline.html

Must .aspx files have a page directive?

Around 90% of the pages for our websites have no .Net code embedded in them yet are published as .aspx files. I want these to render as fast as possible so I'm removing as much as I can.
Does the .Net page directive have an impact on performance? I am thinking about two factors; the page speed for each GET and what happens when the file changes. The CMS system re-creates each page daily and I'm wondering if this triggers the ASP.Net compilation process.
If your pages have no .NET code and rendering speed is your goal, you may wish to consider changing the extension to .html. Any .aspx page will be passed to the .NET ISAPI filter by IIS and go through the entire chain of HttpModules, then will be handled by the Page HttpHandler. Using a .html extension would trigger IIS to process the request using the Static Resource ISAPI filter, which has a much shorter pipeline and is tuned for resources that run no code.
The <%# Page %> directive is not required. Without it, the default values for Language and other stuff will be assumed.
By changing a .aspx file, it'll be recompiled (it doesn't recompile the whole app though):
Any changes to a dynamically compiled file will automatically invalidate the file's cached compiled assembly and trigger recompilation of all affected resources. The next time a request to the code is made, ASP.NET recognizes that the code has changed and recompiles the affected resources of the Web application. This system enables you to quickly develop applications with a minimum of compilation processing overhead. (Note that depending on the change to the resources, the result can range from recompiling a single page to recompiling the whole Web site.)
Ok, put them in - just to be sure.
.NET pages are ALL compiled, page directive or not. nothing changes. Post compilation, they are as fast as it goes, as they turn into a class (type loaded once) that just executes.
Note that the post by Mehrdad Afshari is factually wrong (sadly I can not tag it). Any page change triggers a complete recompile AND restart of the appdomain. Acutally any FILE change does so, as long as it is outside App_Data and ASP.NET can see it (i.e. non-hidden flag on the directory / file).

Resources