Good day. I make a site on ASP.NET MVC 5. In View there is a button (form)

@using (Html.BeginForm("AddToCart", "Cart", FormMethod.Post)) { <div class="pull-right"> @Html.HiddenFor(b=> b.Id) @Html.Hidden("returnUrl", Request.Url.PathAndQuery) <input type="submit" class="btn btn-success" value="Добавить в корзину"/> </div> } 

Accordingly, it generates a POST request to the Cart controller, the AddToCart method. the problem is that when you click on the button, a routing error occurs and the AddToCart method is not called. ERROR: "The matched route value does not include a 'controller' route value, which is required."

Routing Settings

  routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // URL:"/" - Выводит первую страницу списка товаров всех категорий routes.MapRoute( name: null, url: "", defaults: new { controller = "Books", action = "List", page = 1, genre = (string)null } ); //URL: "/PageX" - Выводит страницу X, отображая товары всех категорий routes.MapRoute( name: null, url: "Page{page}", defaults: new { controller = "Books", action = "List", genre = (string)null }, constraints: new { page = @"\d+" } ); // URL:"/категория" - Отбражает первую страницу элементов указанной категории routes.MapRoute( name: null, url: "{genre}", defaults: new { controller = "Books", action = "List", page = 1 } ); // URL:"/категория/PageX" - Отбражает заданную страницу элементов указанной категории routes.MapRoute( name: null, url: "{genre}/Page{page}", defaults: new { controller = "Books", action = "List" }, constraints: new { page = @"\d+" } ); routes.MapRoute( name: null, url: "Nav/Menu" ); 

I did not register routes for this controller. Tell me how to make a universal route for such calls through the form?

  • A universal route is generated when creating a new project. Create a new project and copy from it :) - Pavel Mayorov
  • and where to insert it in the beginning? then the rest are not working at the end does not help - Aldmi
  • There are not many options, you tried one and it didn’t fit, try the second one :) - Pavel Mayorov
  • routes.MapRoute (name: "Default", url: "{controller} / {action} / {id}", defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}); - Aldmi
  • where to put this route? or say not universal for this particular method of action - Aldmi

2 answers 2

The routing system always finds the first match and uses it; the rest are ignored. Which of this should be concluded? - well, for example, that if you put a more general route first, then it will not reach a more specific one, which means that you need to be the first to determine the most specific routes .

In your case, the first route is quite common, and it should be lowered down. Universal ( routes.MapRoute( name: "Default", url: "{controller}/{action}"); ) In any case, the route must be set before the first one, but if you put it (universal) first, it will intercept some others. ( "{genre}/Page{page}" , for example) - in short, the two of them should be moved down, but the universal one should be higher than ""

You can still use named routes, but they should be avoided.

  • one
    can you explain why it’s better to avoid named routes ? - Bald
  • @Bald, yes, it could: because it violates the principle of sharing responsibility. When you create a link in a view, the view doesn't need to know anything about any routs there, it creates unnecessary dependencies. Imagine that this route must be removed / replaced in the course of development - now you will have to be in discomfort: is it not enough where in the view it is used? - this is not what MVC developers wanted - Qutrix
  • The situation described by you is possible without named routes : Html.ActionLink("Метод", "Контроллер") all in the view is protected by information that causes discomfort. - Bald
  • @Bald, there's no getting around here, but fewer dependencies - better, if you can, avoid, with routs there is such an opportunity - Qutrix
  • I think the explanation about the named routes is better to transfer to the answer. thanks for the clarification - Bald
 routes.MapRoute( name: "Default", url: "{controller}/{action}" ); 

This route works. Replaced them with the last route.

  routes.MapRoute( name: null, url: "Nav/Menu" );