Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm not getting friendly url in a form with GET method

I setup a route like that:

routes.MapRoute(
    name: "Pesquisar",
    url: "Pesquisar/{aaa}/{bbb}/{id}",
    defaults: new { controller = "Home", action = "Pesquisar",
                    aaa = UrlParameter.Optional,
                    bbb = UrlParameter.Optional,
                    id = UrlParameter.Optional
    }
);

When I press Send button in a form (with GET method) the url is like that:

http://localhost:00000/Pesquisar?aaa=One&bbb=Two

But I was expecting for:

http://localhost:00000/Pesquisar/One/Two
like image 730
Fabricio Avatar asked Jul 11 '13 01:07

Fabricio


People also ask

Can we use colon in URL?

It is completely fine to use a colon : in a URL path.

How do you get the colon out of the URL?

If you want to include a colon as it is within a REST query URL, escape it by url encoding it into %3A .

How important are SEO friendly URLs?

URLs for SEO PurposesURLs are a good way to signal to a potential site visitor what a page is about. The proper use of URLs can help improve click-through rates wherever the links are shared. And keeping URLs shorter makes them user friendly and easier to share.


1 Answers

When you map a rout, it adds it to the end of a list. When the router looks for the rule to match, it starts at the begining of the list and itterates through it. It will take the first rule that matches, not the most specific rule. Because it is natural to append code to the end, the default rule (which works for almost everything) will be at the start.

Try re-ordering your code to look like this:

        ///The specific rout which you want to use
        routes.MapRoute(
        name: "Pesquisar",
        url: "{action}/{aaa}/{bbb}/{id}",
        defaults: new { controller = "Home", action = "Pesquisar",
                        aaa = UrlParameter.Optional,
                        bbb = UrlParameter.Optional,
                        id = UrlParameter.Optional
        }
        );

        ///The generic catch all router
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

More information can be found in this question:
Setting up ASP.Net MVC 4 Routing with custom segment variables

like image 122
David Colwell Avatar answered Nov 15 '22 04:11

David Colwell