ASP.NET MVC Tips and Reminders (Work in progress)
Expand all | Collapse allRouting
Examples
Default Route
Route with partial parameter defined. Would match
Route with regular expressions Would match
Route with a variable-length list of parameters Would match
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
- ViewResult – Represents HTML and markup.
- RedirectResult – Represents a redirection to a new URL.
- JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
- JavaScriptResult – Represents a JavaScript script.
- ContentResult – Represents a text result.
- FileContentResult – Represents a downloadable file (with the binary content).
- FilePathResult – Represents a downloadable file (with a path).
- FileStreamResult – Represents a downloadable file (with a file stream).
- EmptyResult – Represents no result.
- View – Returns a ViewResult action result.
- Redirect – Returns a RedirectResult action result.
- RedirectToAction – Returns a RedirectToRouteResult action result.
- RedirectToRoute – Returns a RedirectToRouteResult action result.
- Json – Returns a JsonResult action result.
- JavaScriptResult – Returns a JavaScriptResult.
- Content – Returns a ContentResult action result.
- 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") %>