This is a helper extension which automatically registers all website routes containing inside custom attributes in ASP.NET MVC controller methods.

1. Download RiaLibrary.Web.dll and reference it in your project or download source files and include them into your web application.

2. Decorate your controller methods with [Url] attributes:

    public SiteController : Controller
    {
        [Url("")]
        public ActionResult Home()
        {
            return View();
        }

        [Url("about")]
        public ActionResult AboutUs()
        {
            return View();
        }

        [Url("store/{category?}")]
        public ActionResult Products(string category)
        {
            return View();
        }
    }

BTW, '?' sign at the end of '{category?}' parameter means that it's optional UrlParameter.Optional will be a default value for it.

3. Update Global.asax.cs file

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoutes(); // Register Attribute Based Routes which the current assembly contains

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Site", action = "Home", id = UrlParameter.Optional }
            );
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }

That's it! For advanced scenarios go to the Documentation Page or discuss it on ASP.NET MVC Forums

[Page] attribute usage scenarios:

This library also contains [Page] attribute implementation which you may use to link your website pages stored in database (or .xml files etc) with all their metadata (title, description) and assets to the action methods in controllers. Here is how you do it:

    [Page("44ba1f01-bbf4-4f58-a088-0f9566edf968", "Blog Title", "Some Blog Description")]
    public ActionResult Blog()
    {
        return View();
    }


First parameter is Page ID which is not optional. Other two - Page Title, and Page Description are optional parameters. What this action filer attribute does is it retrieves page model from your database (or .xml files) and writes it into ViewData["Page"] variable. Later on you can use this model in your views like this:

<title><%: ViewData.Eval("Page.Title") %></title>


Send your feedback, questions and modification requests to webmaster@riaguy.com

Last edited Feb 20, 2010 at 7:15 AM by koistya, version 34