How to remove the / home / URL of a View specifies ASP.NET MVC5

1

I need to create a View called BancoDeImagens but I need to have it's URL instead of:

  • www.site.com/home/ImageBank

I would like to remove /home/ and separate names by - to look like this:

  • www.site.com/banco-de-imagens

I can not create a view like this:

public ActionResult banco-de-imagens(){

}

My question: Is it possible to map a View so that its URL is the same as the example above?

    
asked by anonymous 29.06.2018 / 20:42

1 answer

4

You can, for this, change your RouteConfig

Probably there will have the following code:

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

Above this code add the following:

 routes.MapRoute(
   name: "BancoImagens",
   url: "banco-de-imagens",
   defaults: new { controller = "Home", action = "BancoImagens", id = UrlParameter.Optional }
);

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

Your action within controller home

public ActionResult BancoImagens(){

}

Note: It is important that the custom routes are above the default route because it will "enter" the first one that "marries"

    
29.06.2018 / 21:04