Search form does not execute controller method

2

I have the following form:

<form role="search" method="get" id="search-form" name="search-form" action="/video/pesquisa">
                        <div class="cover-pursuit-btn">
                            <button type="submit" value="Search" class="pursuit-btn"><span class="glyphicon glyphicon-search"></span></button>
                        </div>
                        <div class="coversquare">
                            <input type="text" class="square fild-placeholder" placeholder="Find in title or description" id="SearchString" name="SearchString" value="">
                        </div>

</form>

My method in the controller is:

[Route("video/pesquisa/{SearchString}/{page?}")]
public async Task<ActionResult> PesquisaVideo(string SearchString, int? page)
{
...
}

When I send the submit it executes the following URL:

http://localhost:59278/video/pesquisa?SearchString=teste

I do not understand why it does not perform correctly, since I already applied the route in the annotation:

http://localhost:59278/video/pesquisa/teste

What am I doing wrong?

UPDATING

This is RouteConfig.cs:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapMvcAttributeRoutes();

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

        }
    
asked by anonymous 16.11.2015 / 20:26

1 answer

2

Nothing. Everything is alright. This is because its action of form is set like this:

<form role="search" method="get" id="search-form" name="search-form" action="/video/pesquisa">

GET attributes do not fit the route formula, however much the route is correct. This is due to the fact that a GET attribute is not a route identifier, such as the {controller}/{action}/{id} cliché.

What happens in this cliché case is that the route interpreter analyzes the address and considers part of the route implicitly as an attribute. That is, id is not considered a parameter of a form GET : it turns a parameter because its route says it.

Now, in your case, you are explicitly using a form and pointing to a action that does not know the {SearchString} route value. In this case it is a bad practice because the GET form asks for free variables, and you are forcing a variable to exist, especially within a route, which is not recognized by the GET protocol (note that GET protocol is different of the ASP.NET MVC framework).

    
16.11.2015 / 20:43