Skip to content
This repository was archived by the owner on Nov 21, 2022. It is now read-only.

Routing

Lodewijk Sioen edited this page Feb 11, 2015 · 3 revisions

Shared Routing

Routing is a central concept in Mvc. It proved to be popuplar that by .net 4.0, Microsoft moved routing into System.Web, making it available for Webforms.

This is how you can define routes for both Webforms and Mvc pages in the same application:

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

        //Route to a Webforms page
        routes.MapPageRoute(
        	"Categories",											// Route name 
        	"Category/{action}/{categoryName}",						// URL with parameters
        	"~/categoriespage.aspx",								// Physical file
        	true,													// Check if user has access to physical file
        	new { action = "Index", categoryName = "home" }			// Parameter defaults
        );

        //Route to a View on an Mvc Controller
        routes.MapRoute(
            "Default",                                              // Route name 
            "{controller}/{action}/{id}",                           // URL with parameters 
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

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

More info on MSDN

Clone this wiki locally