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.
Result:
The value of Id = 2
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article