Click Here to Download SetDefaultRouteInMVC.zip Example.
You can set Default route value for URL in MVC. Traditional our route pattern is like '{controller}/{action}/{id}'. This is going to fail if we pass URL like '{controller}/{action}'.
There are many scenarios in our day to day software application we require both pattern. For that you can set 'Id' as default parameter so that if we did not pass 'id' in query string it will not raise errors and execute our desired 'action'.
We can configure Route in 'App_Start/RouteConfig.cs' file using this 'routes.MapRoute' method, where 'routes' is the object of 'RouteCollection' class. In 'MapRoute' method we can set Default values. Code is something like below :
You can set Default route value for URL in MVC. Traditional our route pattern is like '{controller}/{action}/{id}'. This is going to fail if we pass URL like '{controller}/{action}'.
There are many scenarios in our day to day software application we require both pattern. For that you can set 'Id' as default parameter so that if we did not pass 'id' in query string it will not raise errors and execute our desired 'action'.
We can configure Route in 'App_Start/RouteConfig.cs' file using this 'routes.MapRoute' method, where 'routes' is the object of 'RouteCollection' class. In 'MapRoute' method we can set Default values. Code is something like below :
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { id = UrlParameter.Optional } );
Here is example for this.