ASP.NET MVC - An Introduction

This article will not cover the usual basic ingredients for your first MVC site, it covers the basics you'll need to know before you even start thinking about your first model-view-controller driven site.

The reason of this article is that I find many new web developers fail when it comes to clean web site development. This is a bold statement, but one that, from my experience, needs more attention. [More]

Tags: ,

Development

More Vici tips coming up

In the near future I will be posting some more Vici Project tips & tricks for you all to enjoy.

Tags:

Development

Grouping objects with LINQ in C#

A collegue of mine needed an algorithm to filter out duplicate objects in a List and count the total occurence of each distinct object. He used this query to get his results, it may be usefull to some of you.   myRoles = from c in allRoles group c.RoleId by new { c.PersonId, c.RoleTypeId } into g orderby g.Count() descending select g;   Credits go to Roel Dieltjens

Tags:

Development

Vici Core Tip #1 - XML file based settings

.NET offers quite an extensive configuration framework in which you can use XML files to configure your applications. The drawback is that most of these files need to be located in the applications directory or sub-directories. Thus excluding reuse throughout different applications. An example of this is using an app.config or web.config file to store all your config information. If you have different applications that use the same base assemblies you can either place them in the GAC or place them in the same application directory as you main applications. Which imply that you need to keep you config settings in sync across multiple files and apps. You can off course design your own settings-framework to resolve the above issue or use one of the fancy features of Vici Core. Let's get started on how we implement this. What you need to obtain first is a copy of the latest Vici.Core dll which you can download at http://viciproject.com/wiki/Projects/Mvc/Download (the core has not yet received its own download, so it comes together with the mvc binaries for now, just get it from that download). After you downoaded Vici.Core.dll, reference it in your project and then let the fun begin. I will include some sample code to show you  how simple it is to load your settings in your application Step 1 - Create your XML file As you can see there is a version attribute in the XML file to tell the config framework that we updated the XML file and that a new version needs to be updated in the cache. <?xml version="1.0" encoding="utf-8" ?><Config version="1"> <FileLocation>c:\files\</FileLocation> <TempPath>c:\temp\</TempPath></Config> Step 2 - Create a POCO to hold your settings Property names map to the XML nodes in your config file.   public class ConfigSet{ public string FileLocation; public string TempPath;}   Step 3 - Register your POCO with the Vici Core config framework   using Vici.Core.Config; class Program { public static ConfigSet Config; static void Main(string[] args) { // The path to the location of your config file string configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.xml"); // Register the provider with the xml file ConfigManager.RegisterProvider(new ConfigurationProviderXmlConfig(configFilePath)); // Create a new instance of our POCO Config = new ConfigSet(); // Register our POCO with the framework ConfigManager.Register(Config); // Update our manager to tell it that we registered a new config file ConfigManager.Update(); } }     Step 4 - Use it /* Your code here*/string filePath = Program.Config.FileLocation;/* More of your code here*/

Tags:

Development

Remove SVN files and folders by a click

So your using SVN as we are? And you need to remove all the SVN files and folders to relocate or just detach your project? Then this trick will save you some time. It adds a 'Delete SVN Folders' entry to your context menu in Windows Explorer.   How does it work? Create a new file with a .reg extension and add the following lines to the file. Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN] @="Delete SVN Folders" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN\command] @="cmd.exe /c \"TITLE Deleting SVN Folders in %1 && COLOR 9A && FOR /r \"%1\" %%f IN (.svn) DO RD /s /q \"%%f\" \""

Tags:

Development

Vici MVC 2.0 released

Last weekend a new 2.0 stable version of Vici MVC was released. So if you're really into web development have a break and visit http://www.viciproject.com/   More Tips & Tricks to come ...

Tags: ,

Development

Vici MVC Tip #2 - ActionResult you say?

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 view plaincopy to clipboardprint 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");       }     }   }  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.

Tags:

Development

Björn Bailleul

Web application engineer and developer interested in creating customer centric applications build for ease of use and efficiency. My experience goes from intranet applications to widely used service websites, product portfolios and e-commerce websites.

Specialties

C#, MVC, AJAX, ASP.NET, SQL Server, SQL Reporting Services, WCF, XML, HTML, JavaScript, CSS, Web Services, Scrum, ...

View Bjorn Bailleul's profile on LinkedIn

Month List