0

this is my custom route.but when I want to use RedirectToAction("action","controller"); it cause error. No route in the route table matches the supplied values. I visited some related post but I could not solve it. How do I can fix it?

routes.MapMvcAttributeRoutes();
routes.LowercaseUrls = true;
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}/{filter1}/{filter2}/{filter3}/{filter4}/{filter5}/{filter6}/{filter7}/{filter8}/{filter9}/",
    defaults: new
    {
        controller = "Shop",
        action = "Category",
        id = UrlParameter.Optional,
        filter1 = UrlParameter.Optional,
        filter2 = UrlParameter.Optional,
        filter3 = UrlParameter.Optional,
        filter4 = UrlParameter.Optional,
        filter5 = UrlParameter.Optional,
        filter6 = UrlParameter.Optional,
        filter7 = UrlParameter.Optional,
        filter8 = UrlParameter.Optional,
        filter9 = UrlParameter.Optional,
    }
);
6
  • 1
    How about trying RedirectToAction("action", "controller", new { id = 0 })? I think there's no route matches with action defined in RedirectToAction, you should create another mapping if you want to use multiple filters (or dynamic filters). Commented Nov 3, 2017 at 6:39
  • @TetsuyaYamamoto tnx. it not worked. How can I create another mapping filter. Commented Nov 3, 2017 at 6:52
  • 1
    Is that route mapping the only one route in the RouteConfig? If my guess is right, there you should consider dividing routes into multiple mappings with different filters instead of using single default route for multiple filters. Commented Nov 3, 2017 at 7:00
  • Not related, but only the last parameter can be marked as UrlParameter.Optional Commented Nov 4, 2017 at 3:20
  • 1
    What you are trying will not work because of this. Commented Nov 4, 2017 at 3:29

1 Answer 1

2

I had a similar problem once with RedirectToAction and found out that you need a valid route registered that leads to that action. in your case, the error occurred because your optional parameter is more than expected amount.

I test code and it works :

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

if you want your own route you can use [Route("")] on top of your Action, at the first Add routes.MapMvcAttributeRoutes(); to RegisterRoutes method in RouteConfig.cs. After that do like below:

[Route("{TestEmployee}/{Index}/{id?}/{filter1?}/{filter2?}/{filter3?}/{filter4?}/{filter5?}/{filter6?}/{filter7?}/{filter8?}/{filter9?}")]
public ActionResult Index()
{
    //do something 
}

then you can redirectToAction :

return RedirectToAction("Index", "TestEmployee" ,routeValues: null);
Sign up to request clarification or add additional context in comments.

2 Comments

how about the situation that routeValues is not null!? I used a string like "1/3/def" (my parameters) but it doesn't work.
@mjyazdani Have you mapped correct MapRoute in your RouteConfig.cs ?!

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.