This is my Route config:
routes.MapRoute(
name: "AdsCategories",
url: "Ads/{key}",
defaults: new { controller = "Ads", action = "Index" },
//constraints: new { key = },
namespaces: new string[] { "Vprok.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Vprok.Controllers"}
);
How to create a constraint "AdsCategories"? I need use default route, if action in controller "Ads" == {key}.
So basically if {key}
is an existing action you want it to be handled by the default route instead of using the Index
action on the Ads
controller?
In order to achieve that you could list all the possible actions in the key constraint:
routes.MapRoute(
name: "AdsCategories",
url: "Ads/{key}",
defaults: new { controller = "Ads", action = "Index" },
constraints: new { key = @"^create|update|delete" },
namespaces: new string[] { "Vprok.Controllers" }
);
In this example the AdsCategories
route will match only if the url is not ads/create
or ads/update
or ads/delete
. For example ads/foobar
will match.
take the following code out:
routes.MapRoute(
name: "AdsCategories",
url: "Ads/{key}",
defaults: new { controller = "Ads", action = "Index" },
//constraints: new { key = },
namespaces: new string[] { "Vprok.Controllers" }
);
just create an index actionresult on ads controller and add a id parameter to it! DONE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With