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

Asked

Viewed 40 times

1

I need to create a View called BancoDeImagens but I need her URL instead of being:

  • www.site.com/home/Bancodeimages

I would like to remove the /home/ and separate the names by - to keep it that way:

  • www.site.com/image bank

I can’t create a view like this:

public ActionResult banco-de-imagens(){

}

My question: It is possible to map a View so that its URL matches the example above?

1 answer

4


It is possible yes, for that change your RouteConfig

You’ll probably have the following code there:

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 the controller home

public ActionResult BancoImagens(){

}

Obs: It is important that custom routes stay above the route default because he will "enter" into the first one that "marries"

  • 1

    Perfect ! I had already used the mapping... but I don’t know why it didn’t occur to me to do this hehe. Thanks !

  • 1

    Turns out kkk I was doing 1 here, here came the xD question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.