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
C#
x
33
33
1
using System;
2
3
using System.Collections.Generic;
4
5
using System.Linq;
6
7
using System.Web;
8
9
using System.Web.Mvc;
10
11
using System.Web.Routing;
12
13
namespace MvcApplication1
14
15
{
16
17
public class RouteConfig
18
19
{
20
21
public static void RegisterRoutes(RouteCollection routes)
22
23
{
24
25
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
26
27
routes.MapRoute("NewRoute", "Abundantcode/{controller}/{action}/{id}", new {id = UrlParameter.Optional });
28
29
}
30
31
}
32
33
}
Leave Your Comment