Posted on 20. May 2009 10:01 by B-Virtual
The latest Vici MVC releases from 2080 up contain a new ActionResult object. This allows your controllers to return an action instead of the default void. So what should I use it for? Let's say you would like to stream xml or the contents of a file to the browser. This could be done the old way by using the response object from the WebAppContext or by using the new way using ActionResults.
Also for some ajax requests you expect JSON to be returned by the server in order to make your scripts work. You could do the conversion of your data to JSON by yourself or use anonymous types and the new JSONActionResult to convert your data to JSON automatically.
ActionResults currently available
- JSONActionResult: Output your data as a JSON formatted string for AJAX or Javascript use.
- RedirectActionResult: Redirect to another URL.
- RenderViewActionResult: In fact this is the same as you would use the ChangeLayout method. You define a different view from the default for your controller. As an extra you can provide a complete new ViewData collection to be used by the new view.
- SendFileActionResult: Send a file through the browser to the user
- XmlActionResult: Stream an XML string to the browser
In theory this is all very nice, but how do I use this in code? Well here we go.
Stream a file to the browser
- using Vici.Mvc;
-
- namespace BVirtual.ViciDemo
- {
- [Url("/Download/{DownloadUid}")]
- public class Download : BaseController
- {
-
-
-
-
- public ActionResult Run(string DownloadUid)
- {
-
- DownloadFile dFile = DownloadService.GetFileLocationForUid(DownloadUid);
-
-
- if (dFile != null)
- {
-
-
-
-
- return new SendFileActionResult(dfile.PhysicalLocationOndisk, dFile.MimeType, dFile.Filename);
- }
- else
- {
-
- return _RedirectToErrorPage();
- }
-
- }
-
-
-
-
-
- private ActionResult _RedirectToErrorPage()
- {
-
- return new RedirectActionResult("/Messages/DownloadFailed");
- }
- }
- }
using Vici.Mvc;namespace BVirtual.ViciDemo{ [Url("/Download/{DownloadUid}")] public class Download : BaseController { /// <summary> /// Default action for the download controller /// </summary> /// <param name="DownloadUid">Paramter passed in by Vici MVC during URL parsing</param> public ActionResult Run(string DownloadUid) { // Get the object describing the file we would like to stream from disk DownloadFile dFile = DownloadService.GetFileLocationForUid(DownloadUid); // Check if we found the file in our system if (dFile != null) { // Now start streaming the contents of the file from disk to the browser using // - The location on disk // - The mime type of the file to get the correct program to handle it on the client computer // - The filename of the file you would like to stream return new SendFileActionResult(dfile.PhysicalLocationOndisk, dFile.MimeType, dFile.Filename); } else { // No file found by the given UID, redirect to the error page return _RedirectToErrorPage(); } } /// <summary> /// Example method to show that you can return ActionResult objects from another method or class /// </summary> /// <returns>The action result to be returned by the view</returns> private ActionResult _RedirectToErrorPage() { // Return a redirect action in case of error return new RedirectActionResult("/Messages/DownloadFailed"); } }}
In this example we would like to stream a file from the server to the client browser. In case we couldn't find we redirect to user to an error message page.