Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What clever things have you done with an ASP.NET MVC Action method

Tags:

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!

like image 541
Simon_Weaver Avatar asked Jan 28 '09 01:01

Simon_Weaver


People also ask

What is the use of action method in MVC?

ASP.NET MVC Action Methods are responsible to execute requests and generate responses to it. By default, it generates a response in the form of ActionResult. Actions typically have a one-to-one mapping with user interactions.

Why do we use action method?

Controllers process incoming requests using action methods. Action methods typically have a one-to-one mapping with user interactions. When a user enters a URL into the browser, the MVC application uses routing rules that are defined in the Global. asax file to parse the URL and to determine the path of the controller.

What is ASP Net What are the action results you know?

An action result is what a controller action returns in response to a browser request. The ASP.NET MVC framework supports several types of action results including: ViewResult - Represents HTML and markup. EmptyResult - Represents no result.

What are the 3 main components of an ASP NET MVC application?

The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating MVC-based Web applications.


1 Answers

Does a HTTP 301 Redirect count as clever?

public class PermanentRedirectResult : ActionResult {     public string Url { get; set; }      public PermanentRedirectResult(string url)     {         if (string.IsNullOrEmpty(url))         {             throw new ArgumentException("url is null or empty", "url");         }         this.Url = url;     }       public override void ExecuteResult(ControllerContext context)     {         if (context == null)         {             throw new ArgumentNullException("context");         }         context.HttpContext.Response.StatusCode = 301;         context.HttpContext.Response.RedirectLocation = Url;     } } 
like image 112
Michael Stum Avatar answered Sep 23 '22 06:09

Michael Stum