Getting Started with ASP.NET MVC Framework

Today, I played around with the new ASP.NET MVC Framework. I have to admit it is quite intriguing. I created a small application in which the user can select a particular category and view all the articles related to that category. When the user clicks on a particular article the application redirect the user to the appropriate article. The first thing is to create a CategoriesController which managers the categories. Here is the implementation.

public class CategoriesController: Controller     {

        [ControllerAction]         public void Index         {                    }

        [ControllerAction]         public void List

        {             var categories = CategoryRepository.GetAll;             ViewData.Add("Categories", categories);             RenderView("Categories", categories);         }

           }

The List action gets the categories list from the database and renders the "Categories" view display the categories. The Categories view looks something like the following:

 public partial class Categories: ViewPage<IEnumerable<GridViewGuyWebApps.Models.Category>>     {

    }

The ViewPage accepts an IEnumerable which means we will be able to access the ViewData bag in the form of the IEnumerable. Now, let's take a look at the HTML part of the view:

<% foreach (var category in ViewData)        {                  %>            

  •       <%=  Html.ActionLink(category.Title, new { controller = "Articles", action = "List", category = (category.Title.Replace(".","_")) }) %>      
  •               <% } %>

    The foreach loop simply iterated over the ViewData which in this case is IEnumerable and created links to navigate to the articles contained in that category. Some of the category titles contained "." so I just replaced them with the "_" no biggy deal there!

    The second argument to the Html.ActionLink method is pretty cool! It allows you to write your own URL. The URL that I am interested is the following:

    http://YourWebApplication/Articles/List/[Category]

    This means if I have some articles which belong to the "Security" category then my URL will be something like:

    For this to work correctly you will need to modify the Global.asax file and add a route.

     protected void Application_Start(object sender, EventArgs e)         {             // Note: Change Url= to Url="[controller].mvc/[action]/[id]" to enable             //       automatic support on IIS6

                RouteTable.Routes.Add(new Route             {                 Url = "Articles/List/[category]",                 Defaults = new { controller = "Articles", action = "List", category = (string)null },                 RouteHandler = typeof(MvcRouteHandler)             });

                RouteTable.Routes.Add(new Route             {                 Url = "[controller]/[action]/[id]",                 Defaults = new { action = "Index", id = (string)null },                 RouteHandler = typeof(MvcRouteHandler)             });

                RouteTable.Routes.Add(new Route             {                 Url = "Default.aspx",                 Defaults = new { controller = "Home", action = "Index", id = (string)null },                 RouteHandler = typeof(MvcRouteHandler)             });

            }

    Url = "Articles/List/[category]" means that the List action of the Articles controller will take the "category" as the parameter. Now, let's take a look at the List action of the ArticlesController.

    public class ArticlesController: Controller     {

            [ControllerAction]         public void Index         {             //Add action logic here         }

            [ControllerAction]         public void List(string category)         {             var articles = ArticleRepository.GetByCategoryName(category.Replace("_","."));             ViewData.Add("Articles", articles);             RenderView("Articles",articles);         }

            [ControllerAction]         public void Details(int id)         {             var article = ArticleRepository.GetById(id);             ViewData.Add("Article", article);             RenderView("Details", article);         }     }

    That is pretty much it. I am in the process of writing a new article on ASP.NET MVC Framework in which I will cover the complete details.

    This article is part of the GWB Archives. Original Author: Mohammad Azam

    New on Geeks with Blogs

    • We Won The One Award I Actually Care About

      Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

    • Your Customers Build the Features Now

      I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

    • Get the Size of a Directory in Linux the Easy Way

      du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

    • Vim Search and Replace: The Ultimate Guide

      One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.