title: What clever things have you done with an ASP.NET MVC Action method
body: <p>The ASP.NET MVC controller action methods are primarily used for handling 'business' operations but it can be used for lots more.</p>
  
  <p>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.</p>
  
  <p>Here's my contribution :</p>
  
  <p><strong>Javascript file concatenator - to <a href="http://developer.yahoo.com/performance/rules.html#num_http" rel="nofollow noreferrer">reduce number of http requests:</a></strong></p>
  
  <pre><code>    [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"
          };
      }
  </code></pre>
  
  <p>Map a route to it :</p>
  
  <pre><code>  // javascript
    routes.MapRoute(
       "js-route",
       "dynamic/js",
       new { controller = "Application", action = "RenderJavascript" }
    );
  </code></pre>
  
  <p>Refer to it from your master page :</p>
  
  <pre><code>    &lt;script type="text/javascript" src="/dynamic/js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>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!</p>
  
  <p>I jsut need to come back and figure out how to gzip it.</p>
  
  <p><code>*</code> You shouldn't use VaryByParam="None" because that causes the Vary header to be send, which <a href="https://stackoverflow.com/questions/376851/expire-output-cache-asp-net-mvc#486072">causes the browser to go back and check for a new version</a>. If you really have to change your js content then your users are just goin to have to wait 5 minutes for it!</p>