Optional URL Segments in ASP.NET MVC

The developer can define the optional URL segment which need not be specified by the user when entering the URL. For example , assume the URL pattern “Abundantcode/{controller}/{action}/{id}” . In this pattern , the developer can make the id segment as optional by specifying them in the MapRoute method .

Optional URL Segments in ASP.NET MVC

Below is a code snippet demonstrating the usage of Optional URL Segments in ASP.NET MVC

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing;

namespace MvcApplication1

{

public class RouteConfig

{

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute("NewRoute", "Abundantcode/{controller}/{action}/{id}", new {id = UrlParameter.Optional });

}

}

}