First of all, it doesn't make any sense to have optional route values in this case. Optional parameters only work if they are the right-most parameter followed by nothing more than another optional parameter or /
. You have dashes in your URL, making this impossible. So, remove the optional declaration in your route:
routes.MapRoute(
name: "NewRoute",
url: "P/{colorname}-{id}/{statename}-{materialtype}-{HasPrice}",
defaults: new
{
controller = "Products",
action = "P"
}
);
You must also ensure that your route can be matched (that is, no route that is registered before it will cause it to be unreachable). In plain English, this means you must define the above route before your Default route.
Note that because none of the parameters are optional, the route will always be skipped if there are any missing ones - this is desirable behavior because there may be additional routes in your configuration that you want to match that are registered after this one.
Now, to generate the URL you want, you need to provide the missing route data at the time the URL is generated. For example, this is how you would generate a hyperlink to the URL in your question using the above route:
@Html.ActionLink("My Link", "P", "Products",
new { colorname = "رنگ", id = "شماره", statename = "نام استان",
materialtype = "نوع مواد", HasPrice = "قیمت" }, null)
This of course assumes you have already made a controller named ProductsController
that has an action named P
in it.