Thursday, 14 October 2010

MVC1 to MVC2 caching gotcha

I’m presently helping a client refresh their solution to the latest technology in K2, ASP.NET MVC, Sharepoint etc and found quite a poor issue with how caching works in MVC 2 compared to MVC 1.

The scenario, lets say we have a single controller with InitialiseA(), InitialiseB() and Start(). The two init methods would set some session variables or do something in the database and then send back a redirect to action to go off to Start. This is what I expect to happen;

The reality is significantly different though. The above is how it would work in MVC1, but in MVC2, the following happens;

The result of the first invocation of Start is being cached and so, when the browser gets the 302 response it realises it already has the content for the redirected url and just renders that. So what’s changed?

MVC1 used to send an expires HTTP response header set to the current time, meaning  the browser wouldn’t cache the result. MVC 2 however doesn’t send this by default, so I found myself with a bunch of issues around the scenario described above. In just so happens that in this case all of my controllers descend from one common abstract base controller, so I was able to add the OutputCache(Location=OutputCacheLocation.None) attribute to this base class as follows;

[OutputCache(Location=OutputCacheLocation.None)]
public abstract class MyBaseController : Framework.Web.Mvc.ControllerBase
{
..
}

Sunday, 3 October 2010

Get executable full path?

Today I needed to know where a command line executable was running from – msbuild.exe to be precise. My path environment variable is extremely long on my dev box, so I just wanted a quick way to find out where the exe would be run from. The following does exactly what I needed;

for %f in (msbuild.exe) do echo %~$PATH:f

And the output?

image

Just what I needed :)