Route not allowed

I want clean urls and 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 common search bar on my main page with a text box and a submit button. He goes to /Search. The name of the text field query. Everything is excellent and excellent. When I click Search, my first action is executed but not executed when called RedirectToAction():

No route in the route table matches the specified values.

I can not find the reason why it does not work.

+3
source share
2 answers

content, query index . , , , . .

+2

, , , , - .

, MVC 2, -.

(, ), string.Empty,

content = UrlParameter.Optional

, MVC 1.

+2

Source: https://habr.com/ru/post/1732014/


All Articles