The ASP.NET MVC controller action methods are primarily used for handling 'business' operations but it can be used for lots more.
I thought it would be fun to see what creative, useful things people have created actions for that may be practical or useful for others.
Here's my contribution :
Javascript file concatenator - to reduce number of http requests:
[OutputCache(Duration = 5 * 60, VaryByParam="")] // DONT USE "None" here *
public ContentResult RenderJavascript(){
StringBuilder js = new StringBuilder();
StringWriter sw = new StringWriter(js);
// load all my javascript files
js.AppendLine(File.ReadAllText(Request.MapPath("~/Scripts/jquery.hoverIntent.minified.js")));
js.AppendLine(File.ReadAllText(Request.MapPath("~/Scripts/jquery.corner.js")));
js.AppendLine(File.ReadAllText(Request.MapPath("~/Scripts/rollingrazor.js")));
return new ContentResult()
{
Content = js.ToString(),
ContentType = "application/x-javascript"
};
}
Map a route to it :
// javascript
routes.MapRoute(
"js-route",
"dynamic/js",
new { controller = "Application", action = "RenderJavascript" }
);
Refer to it from your master page :
<script type="text/javascript" src="/dynamic/js"></script>
Be warned I've set a cache for the output, so if you're changing your JS and refreshing the page you might want to disable the cache!
I jsut need to come back and figure out how to gzip it.
*
You shouldn't use VaryByParam="None" because that causes the Vary header to be send, which causes the browser to go back and check for a new version. If you really have to change your js content then your users are just goin to have to wait 5 minutes for it!