0

I'm new in MVC, My route defined as follows in:

   routes.MapRoute(
            name: "NewRoute",
            url: "P/{colorname}-{id}/{statename}-{materialtype}-{HasPrice}", 
        defaults: new
            {
                controller = "Products",
                action = "P", 
                id = UrlParameter.Optional, 
                statename = UrlParameter.Optional,
                colorname = UrlParameter.Optional,
                materialtype = UrlParameter.Optional,
                HasPrice = UrlParameter.Optional

            }
        );

when user search in my website ,i want show Below URL:

P/قیمت-نوع مواد-نام استان/شماره-رنگ

but get 404 error and show this URL: /P/-/--

Please help me... thanks...

M.h Basiri
  • 56
  • 9
R.Mrz
  • 1
  • 6

1 Answers1

0

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.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Thanks for your detailed explanation... but when i used this ActionLink , it goes to myDefault route...my Default route define after NewRoute..Please Help me... – R.Mrz Apr 30 '17 at 08:07
  • It looks like your words may also have spaces between them, which is not allowed in a URL. See [(Please) Stop Using Unsafe Characters in URLs](https://perishablepress.com/stop-using-unsafe-characters-in-urls/) for a breakdown of what characters are allowed and not allowed in URLs. I suggest you use a placeholder character or token for a space and have your application do a replacement for the token before it gets to your controller, such as in a value provider or filter. – NightOwl888 May 04 '17 at 08:11