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<Category>();
            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<Category> which means we will be able to access the ViewData bag in the form of the IEnumerable<Category>. Now, let's take a look at the HTML part of the view:

<% foreach (var category in ViewData)
       {
          
      %>
     
      <li>
      <%=  Html.ActionLink(category.Title, new { controller = "Articles", action = "List", category = (category.Title.Replace(".","_")) }) %>
      </li>
      
       <% } %>

The foreach loop simply iterated over the ViewData which in this case is IEnumerable<Category> 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:

http://YourWebApplication/Articles/List/Security

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.

Print | posted @ Tuesday, January 29, 2008 1:03 PM

Twitter