Why error The resource could not be found for
routes.MapRoute(
"Product",
"Product/{productId}",
new {controller="Product", action="Details"},
new {productId = @"\d+" }
);
when url is http://website.com/product/1a
Answer : Your getting error because when you apply constrain on the route of url , if constrain doesnt match MVC just reject that request ..that is the only reason you are getting error of resource not found.
why error The parameters dictionary contains a null entry for parameter 'productId' of non-nullable type 'System.Int32' for method for
routes.MapRoute(
"Product",
"Product/{action}/{productId}",
new {controller="Product", action="Details"}
);
when url http://website.com/Product/Details/1a
Answer : in this case no routconstrain is applied so ModelBinder try to match parameter using DefaultValuProvider if it fails to match value to the parameter than it throws error as you are getting here as no conversion means null.
To avoid this error you can try this
a. pass defualt value to action method
public ActionResult Index(int id=0)
b. create method with nullable parameter so null get handled automaticalyy
public ActionResult Index(int? id)
Problem is not because of constrain route
routes.MapRoute(
"Product",
"Product/{productId}",
new {controller="Product", action="Details"},
new {productId = @"\d+" }
);
as per above code , you are looking for product id which is integer so if you provide string like http://website.com/Product/Details/1a it tries to match first value with first page holder means whatever after product matched with productId in this case...for matching this mvc use ModuleBinder when module binder finds its string not int i.e. not able to convert string in int it throws error you are getting.
So for as per your route it matches Details with product id but not able to find match for 1a that is the reason you are getting resource not found.
When you have route like this Product/{action}/{productId} and call url like this http://website.com/Product/Details/1a it matches Details with {action} and 1a with {ProductId} than you got error The parameters dictionary contains a null entry for parameter 'productId' of non-nullable type 'System.Int32' for method