4

I tried to create an example from this website.

They've defined a route like:

routes.MapRoute(
    "Product",
    "Product/{productId}",
    new {controller="Product", action="Details"},
    new {productId = @"\d+" }
);

It works mean The resource could not be found error will occur when productId has no integer value. (I accessed http://website.com/product/1a then error is displayed otherwise will display a view)

But if I change the url format from route into like this:

"Product/{action}/{productId}"

And access it like: http://website.com/Product/Details/1a , then an error is occurred like: The parameters dictionary contains a null entry for parameter 'productId' of non-nullable type 'System.Int32' for method

So, why is not displayed the The resource could not be found error ? Why action is reached while I put a constraint to route ?

PS: I change the url format for route and now it looks like:

routes.MapRoute(
    "Product",
    "Product/{action}/{productId}",
    new {controller="Product", action="Details"},
    new {productId = @"\d+" }
);
3
  • What did you change exactly? How does the route look like? Commented Jul 9, 2015 at 8:56
  • 1
    See updated question. I already explained what I changed. Commented Jul 9, 2015 at 8:58
  • is that answer helpful to you i updated my answer why you got error ... Commented Jul 9, 2015 at 10:11

2 Answers 2

2

The reason for this is not the route you specified but another route item in your code, most likely the default MVC route:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

When the value for productId doesn't match the constraint, the route engine moves on to check the next mapping. It then matches the last one, but when attempting to call your method, the model binder can't convert the string 1a to an int which effectively means that the productId parameter is missing.

Sign up to request clarification or add additional context in comments.

7 Comments

The id is optional (in the default route) and should not throw this exception if it's null. After all the id is provided and isn't missing..
Because id is optional, that is why this route would match.
I agree with you, the route would match. But the id is provided and not missing.
Exactly, that's why the error happens and comes from the controller action invoker rather than a 404.
It's missing because the model binder can't convert 1a into a non nullable int.
|
1

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

3 Comments

That's the reason he is not getting the resource not found error ;)
For first example, I didn't access http://website.com/Product/Details/1a but http://website.com/Product/1a as you see that Details is missing (that's why works)
@SnakeEyes - Check the answer that is the thing you might looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.