FreeKB - MVC Routing
MVC - Routing

A new MVC project will automatically create the Home Controller and View. A few different URLs will all produce the same page:



This is made possible by MVC Routing. The following line in the Global.asax.cs file make is possilbe to use Routing.



The RouteConfig.cs file under the App_Start folder contains the specific configuration settings.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

    public class RouteConfig
        public static void RegisterRoutes(RouteCollection routes)

                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }


Notice in this example the Default route has controller = "Home" and action = "Index". When is requested, /Index is automatically assumed. Likewise, when is requested, /Home/Index is automatically assumed.

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter c3c76 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |