Base class.
Accepts an object and returns it serialized as a "JSON/text" response.
Example:
public class JsonController : Controller { public class ResultObject { public int resultCode; public string message; public bool success; public DateTime actionTime; } public JSONActionResult Run() { return new JSONActionResult(new ResultObject { actionTime = DateTime.Now, message = "A message", resultCode = 114, success = true }); } }
Response:
{"value":{"resultCode":114,"message":"A message","success":true,"actionTime":new Date(1246972828600)}}
Redirects the client to a new URL. Set permanent = true for a HTTP 301 (Moved Permanently) status code.
Example without permanent redirection:
[Url("oldpage/pageId")] public class PageRedirectController : Controller { public RedirectActionResult Run(string pageId) { return new RedirectActionResult("/newpage/" + pageId); } }
Response:
... HTTP/1.x 302 Found ... Location: /newpage/some_page_id ...
Example with permanent redirection:
[Url("oldpage/pageId")] public class PageRedirectController : Controller { public RedirectActionResult Run(string pageId) { return new RedirectActionResult("/newpage/" + pageId, true); } }
Response:
... HTTP/1.x 301 Moved Permanently ... Location: /newpage/some_page_id ...
Default ActionResult for controllers.
Example as you normally would use a controller:
[Url("somepage")] [View("someview")] public class RenderViewController : Controller { public void Run() { // ... do some logic and set some ViewData } }
Example specifically defining the return type and passing the "View" property to the RenderViewActionResult constructor:
[Url("somepage")] [View("someview")] public class RenderViewController : Controller { public RenderViewActionResult Run() { // ... do some logic and set some ViewData return new RenderViewActionResult(this.View); } }
Example specifically defining the return type, but returning null. This will cause the default view (someview) to be rendered:
[Url("somepage")] [View("someview")] public class RenderViewController : Controller { public RenderViewActionResult Run() { // ... do some logic and set some ViewData return null; } }
Returns a file and sets the specified content type.
Example:
[Url("somepage/file")] public class SendFileController : Controller { public SendFileActionResult Run(string file) { byte[] fileBytes = SomeFileHelper.GetFileBytes(file); return new SendFileActionResult(fileBytes, "application/octet-stream"); } }
Returns a HTTP status code response with an optional message.
Example:
[Url("error/{statusCode}")] public class StatusCodeController : Controller { public StatusCodeActionResult Run(int statusCode) { string message = "Some default message"; // ... some logic to check the passed status code and set a message return new StatusCodeActionResult(statusCode, message); } }
Response (from a call to /error/403) could be:
... HTTP/1.x 403 Forbidden ...
Returns the HTTP 404 (Not Found) status code.
Returns the HTTP 304 (Not Modified) status code.
Example:
[Url("error/{statusCode}")] public class StatusCodeController : Controller { public StatusCodeActionResult Run(int statusCode) { string message = "Some default message"; switch (statusCode) { case 304: return new NotModifiedActionResult(); case 404: return new NotFoundActionResult(); default: return new StatusCodeActionResult(statusCode, message); } } }
Returns an XML response with content type text/xml.
Example:
[Url("xml")] public class XmlController : Controller { public XmlActionResult Run() { string xml = SomeHelper.GenerateXml(); return new XmlActionResult(xml); } }
Response could be something like this:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
</book>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
</book>
</catalog>