Wednesday, March 11, 2009

Caching and Url Mapping


If you look at the line <%@ OutputCache... in the .aspx files you will see that it is configured to cache the output of that whole page for the value in the Duration attribute. The VaryByParam value indicates that any url parameter will cause the page to be re-generated and cached. Replace the "*" with "none" and the caching will ignore the url params and that page will be cached once for the duration.
The OutputCache can be used in individual user controls as well, however, if the page has a duration of 20 and the control within it has a duration of 10 both will be cached for 20. Reverse those numbers and the page will be cached for 10 but the control will be cached for 20.

Be careful that you don't try to reference in your code a control which may have been cached, as it will come back with a null reference, possibly causing an exception!
One other method of caching is substitution mapping, the drawback being that you can only point this at .net controls, not user controls.

Url Mapping.

Now, go to IIS, create a virtual directory under Default Web Site, point it at the Test solution's directory. Open the Properties panel for the virtual dir. Do the following:
Disable directory browsing.
Under Documents, enable default document and have only 1 default document called default.aspx.
Go back to Virtual Directory, click Configuration, click Add and enter:
1. Executable: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
2. Extension: .*
3. All Verbs
4. Script Engine ticked
5. Check that file exists NOT ticked
6. Click Ok, Click Ok.
Go to ASP.NET tab and ensure the ASP.NET version is 2.0.50727
Click Ok.
Now, when you browse to http://localhost/test/lose/ or http://localhost/test/win/ you should see the page come up which is actually /test/win.aspx or /test/lose.aspx. Hit refresh and you will see the time has not changed, because the page has been cached. The page appears, even though you have entered a directory name, because IIS is automatically adding default.aspx to any requested directory listing and redirecting all file requests to the .net framework. The .net framework is redirecting from ~/win/ or ~/lose/ to ~/win.aspx or ~/lose.aspx because in the web.config you have a UrlMapping block! Easy.

The only problem with this is that you can't use regular expressions to have, for example, ~/search/ipod/ re-mapped to ~/search.aspx?query=ipod which is annoying, but only because the next server version from microsoft will have a fully operational url mapper and they didn't have time to put it into the current release.


Substitution mapping:
Url Mapping:
MS Blog:

No comments:

Post a Comment