asp.net MVC

[Solved] The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Index(Int32)'

[Solved] The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Index(Int32)' , someone asked me to explain?
MVC

Problem:

In RouteConfig class by default id is optional parameter. 

public static void RegisterRoutes(RouteCollection routes)
        {
           routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
           routes.MapRoute(
                name: "Default",
               url: "{controller}/{action}/{id}",
               defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        } 

In my homecontroller index function I have a parameter named id as integer.

public class HomeController : Controller
    {
        // GET: /Home/
        public string Index(int id)
        {
            return "The value of Id = " + id;
        }
    } 

When I run the project I got following error.

http://localhost

Server Error in '/' Application.

The parameters dictionary contains a null entry for parameter'id' of non-nullable type 'System.Int32' for method 'System.StringIndex(Int32)' in 'MVC_tutorials.Controllers.HomeController'. An optionalparameter must be a reference type, a nullable type, or be declared as anoptional parameter.
Parameter name: parameters


Description: An unhandled exception occurred during the execution of thecurrent web request. Please review the stack trace for more information aboutthe error and where it originated in the code. 

Exception Details: System.ArgumentException:The parameters dictionary contains a null entry for parameter 'id' ofnon-nullable type 'System.Int32' for method 'System.String Index(Int32)' in'MVC_tutorials.Controllers.HomeController'. An optional parameter must be areference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
 

Solution:

The function is expecting an id parameter it was not supplied. Otherwise we need to change the parameter function as optional, like below function; it will not throw an error. 

public string Index(int? id)
        {
            return "The value of Id = " + id;
        }

Result:

The value of Id =

Eventhough it is expecting a parameter it should be passed as expected.

http://localhost/home/index/2 

Result:

 The value of Id = 2

Post your comments / questions