Vici MVC Tip #2 - ActionResult you say?

20. May 2009

Vici MVC Tip 2The 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

  1. using Vici.Mvc;  
  2.  
  3. namespace BVirtual.ViciDemo  
  4. {  
  5.   [Url("/Download/{DownloadUid}")]  
  6.   public class Download : BaseController  
  7.   {  
  8.     /// <summary>  
  9.     /// Default action for the download controller  
  10.     /// </summary>  
  11.     /// <param name="DownloadUid">Paramter passed in by Vici MVC during URL parsing</param>  
  12.     public ActionResult Run(string DownloadUid)  
  13.     {  
  14.       // Get the object describing the file we would like to stream from disk  
  15.       DownloadFile dFile = DownloadService.GetFileLocationForUid(DownloadUid);  
  16.  
  17.       // Check if we found the file in our system  
  18.       if (dFile != null)  
  19.       {  
  20.         // Now start streaming the contents of the file from disk to the browser using   
  21.         // - The location on disk  
  22.         // - The mime type of the file to get the correct program to handle it on the client computer  
  23.         // - The filename of the file you would like to stream  
  24.         return new SendFileActionResult(dfile.PhysicalLocationOndisk, dFile.MimeType, dFile.Filename);  
  25.       }  
  26.       else 
  27.       {  
  28.         // No file found by the given UID, redirect to the error page  
  29.         return _RedirectToErrorPage();  
  30.       }  
  31.  
  32.     }  
  33.  
  34.     /// <summary>  
  35.     /// Example method to show that you can return ActionResult objects from another method or class  
  36.     /// </summary>  
  37.     /// <returns>The action result to be returned by the view</returns>  
  38.     private ActionResult _RedirectToErrorPage()  
  39.     {  
  40.       // Return a redirect action in case of error  
  41.       return new RedirectActionResult("/Messages/DownloadFailed");  
  42.     }  
  43.   }  

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.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development

What's in a name ...

19. May 2009

kijker Once upon a time there was a young boy that was looking for a virtual outlet on the web. Together with a few colleagues they all started their own websites. But! Wait a second, it all stands with a good name right? Right! So of we went, searching for good names for our sites. And there it was, out of the blue, as a flash of lightning: B-Virtual! Why? Since the web is a virtual place on a cloud of its own and my name starts with a B there it was => B-Virtual. So, years past by, visitors enjoyed this website, postings were done under the name B-Virtual and all went well. I've been active on the web for about 14 years now, I've seen domains come and go, technologies rise and fall, experienced venture capital at its best thanks to HyperTrust and met many interesting people.

 

But here comes the interesting part of the story. A few months ago I was contacted by someone asking me if I was interested in selling my b-virtual.com domain. My first reaction was no, why should I, it's almost my second name. But they kept asking and I went thinking. Since I live in Belgium and I own both .com, .eu and .be domains I started thinking that I could miss the .com if the price was right. Ok, I here you thinking, here we go, he's going to take advantage of the situation. Not really actually, I made some calculations and found a reasonably low price including fees for new logo designs, email campaigns, branding, ... but the price was seen as to high. No problem for me, I'm not really looking to sell it anyway.

 

Since I'm into the web, I went looking for more information about the company wanting to buy my domain. I came up with B-Virtual, a Belgian, yes Belgian as in Belgium, company. So do the math with me, a Belgian company wanting my name for their company? It seems as if the people at A-Server weren't really in a creative mood. They just took my name. Maybe it's good marketing, take a name that has a good SEO rating a piggyback on it. Don't get me wrong, I'm not against their products, cloud services are the future for some virtual needs. It's just that they were to lazy to search for a fresh and creative name for their brand.

 

To proof that I'm in favor of them, I'll even advertise their website, since I'm still, at the time of writing, higher ranked in Google ;)

Here we go:

B-Virtual delivers ground breaking cloud storage solutions

I wish you all great success in your business since I'm also a through believer in virtual storage solutions.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Featured, Varia

Vici MVC Tip #1 - foreach range

18. May 2009

Vici Project Logo

As you all might know, or not know, I'm a hardcore user of the Vici Project. So I will try to post some tips and tricks on Vici projects here.

This piece of code helps those who would like to loop over a range of items or just use a counter to loop over.

 

 

{{foreach i in [1...20]}}
  Insert your HTML template part here
{{endfor}}

Ok, this is easy, it just loops over the numbers 1 to 20 for i. But what if I would like to do it the other way around. Just do.

{{foreach i in [20...1]}}
  Insert your HTML template part here
{{endfor}}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development ,

Garageverkoop te Schelle

4. May 2009

Op zaterdag 9 mei 2009 kan je in Schelle terecht voor hun 5e garageverkoop. Bewoners uit de gemeente kunnen dan hun garage openzetten om naar goede oude amerikaanse, of is het nu schelse, traditie hun overbodige spulletjes te verkopen.

 

Voor meer info en adressen van de garages die meedoen kan je terecht op Schelle Leeft!.

 

Oh ja, vergeet vooral niet onze garage eens een bezoekje te brengen ... voor de ingewijden.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Actua