ASP.NET MVC Tips and Reminders (Work in progress)

Expand all | Collapse all

Routing

Examples

Default Route
                    routes.MapRoute(
                        null, // Route name
                        "{controller}/{action}/{id}", // URL with parameters
                        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
                    );
                

Route with partial parameter defined. Would match /Page1, /PageA, /home/page3
                    routes.MapRoute(
                        null,
                        "Page{page}",
                        new { controller = "Home", action = "Index", id = "" }
                    );
                

Route with regular expressions Would match /Page1, /Page123, but not /PageA
                    routes.MapRoute(
                        null,
                        "Page{page}",
                        new { controller = "Home", action = "Index", id = "" },
                        new { page = @"\d+" }
                    );
                

Route with a variable-length list of parameters Would match /Page1/Page2/Page3/XYZ
                    routes.MapRoute(
                        null,
                        "Page{*pages}",
                        new { controller = "Home", action = "Index", id = "" }
                    );
                

Controllers and Actions

Default Parameter Values for Controller Actions

Don't forget that C# 4's optional parameters can be used in action methods to provide a default value if the parameter wasn't supplied.

public ViewResult SomeAction(int page = 1) {
    // Code ...
}

Action Result Types

  1. ViewResult – Represents HTML and markup.
  2. RedirectResult – Represents a redirection to a new URL.
  3. JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
  4. JavaScriptResult – Represents a JavaScript script.
  5. ContentResult – Represents a text result.
  6. FileContentResult – Represents a downloadable file (with the binary content).
  7. FilePathResult – Represents a downloadable file (with a path).
  8. FileStreamResult – Represents a downloadable file (with a file stream).
  9. EmptyResult – Represents no result.
All of these action results inherit from the base ActionResult class.
  1. View – Returns a ViewResult action result.
  2. Redirect – Returns a RedirectResult action result.
  3. RedirectToAction – Returns a RedirectToRouteResult action result.
  4. RedirectToRoute – Returns a RedirectToRouteResult action result.
  5. Json – Returns a JsonResult action result.
  6. JavaScriptResult – Returns a JavaScriptResult.
  7. Content – Returns a ContentResult action result.
  8. File – Returns a FileContentResult, FilePathResult, or FileStreamResult depending on the parameters passed to the method.

Uploading Files

HTML Form
                        <form enctype="multipart/form-data">
                            <input type="file" name="image"/>
                        </form>
                    
Action Method
                    [HttpPost]
                    public ActionResult DoSomething(HttpPostedFileBase image) {
	                    if (image != null) {
		                    var contentType = image.ContentType;
		                    byte[] data = new byte[image.ContentLength];
		                    image.InputStream.Read(data, 0, image.ContentLength);
	                    }
                    }

                    //Displaying Image
                    public FileResult DisplayImage() {
	                    var image = //Get Image Data Object...
	                    return File(image.Data, image.MimeType);
                    }
                    

Links

HTML Helpers

Notes

  • Register your custom HTML Helpers/Extensions globally by adding a namespace in the web.config.
                                <namespaces>
                                    <add namespace="Something.Whatever.HtmlHelpers" />
                                </namespaces>
                             

Html.RenderAction

Html.RenderAction pipes the output from an arbitrary action method into a view's output. More efficient than Html.Action for large data.
<% Html.RenderAction("action", "controller"); %>
                

Paths

Resolve Virtual Paths with ~

To resolve virtual paths, use <%: Url.Content("~/images/picture.jpg") %>