0

URL rewriting issue. When i call me url using the following link

http://localhost:12719/product/18

it works fine the product 18 links to the parameter. However when i call it using the following.

http://localhost:12719/product/apple

It doesn't map the apple product name to the controller, it thinking that your trying to invoke a action of type apple.

Why does it map numeric and not string to the controller parameter? The controller parameter is an type of string.

Routing are as below.

routes.MapRoute(
    name: "product",
    url: "product/{id}/{slug}",
    defaults: new { controller = "product", action = "product", slug = UrlParameter.Optional },
    constraints: new { id = @"\d+" }
);

1 Answer 1

1

You have specified id to be only numeric in the regex specified in the constraint. constraints: new { id = @"\d+" } remove it and should work. So since "product" doesnot pass \d+ test you will get the id as null in the action.

routes.MapRoute(
    name: "product",
    url: "product/{id}/{slug}",
    defaults: new { controller = "product", action = "product", slug = UrlParameter.Optional }

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

2 Comments

doesn't work :( is there a way to change the regex type to string? error im getting. The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult
Change the type of id from int to string in your action.

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.