Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route not getting resolved

I want clean URLs and have defined two routes:

routes.MapRoute(
    "Search",
    "Search",
    new { controller = "Search", action = "SearchPanel" }
);
routes.MapRoute(
    "SearchResults",
    "Search/{content}",
    new { controller = "Search", action = "Search", content = string.Empty, query = string.Empty, index = 0 }
);

then I have two actions:

[HttpPost]
public ActionResult Search(string content, string query)
{
    if (string.IsNullOrEmpty(query))
    {
        return RedirectToAction("Home", "Application");
    }
    return RedirectToAction("Search", new { content = content, query = query }); ;
}

public ActionResult Search(string content, string query, int? index)
{
    if (string.IsNullOrEmpty(query))
    {
        return RedirectToAction("Home", "Application");
    }

    switch (content)
    {
        case "products":
            // get products
            return View("ResultsProducts");
        case "categories":
            // get categories
            return View("ResultsCategories");
        default:
            // get all
            return View("ResultsAll");
    }
}

I have a generic search panel in my master page that has a textbox and a submit button. It posts to /Search. Textbox's name is query. All fine and great. When I hit Search my first action gets executed, but fails on RedirectToAction() call:

No route in the route table matches the supplied values.

I can't seem to find the reason why it doesn't work.

like image 890
Robert Koritnik Avatar asked Aug 19 '26 04:08

Robert Koritnik


2 Answers

Removing content, query and index from defaults in second route, resolved the problem. Why is that I can't really tell, because those just define defaults, when they're not provided which in my case is not the case. I'm providing those values anyway.

like image 185
Robert Koritnik Avatar answered Aug 20 '26 21:08

Robert Koritnik


I had the same problem, and thankfully this helped me so I want to give something back.

It seems there was a change in the MVC 2 framework which forces you to declare the routes in a different way.

In order to have the extra route values there (e.g. content) you must not assign the default of string.Empty, but rather have

content = UrlParameter.Optional

This should then allow your route evaluations to behave like it did in MVC 1.

like image 39
Wiaan Avatar answered Aug 20 '26 22:08

Wiaan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!