1
I have a problem with automatic url generation next to Asp.NET
I have the following code:
[RoutePrefix("c")]
[Route("{action=Index}")]
public class ConteudoController : BaseController
{
[Route("{urlConteudo}")]
public ActionResult Index(string urlConteudo){ ... }
[Route("{urlMenu}/{urlConteudo}")]
public ActionResult Index(string urlMenu, string urlConteudo) { ... }
}
When generating the url use the following code snippet
Url.Action("Index", "Conteudo", new { urlConteudo= "titulo-teste", urlMenu = "noticias" });
But the result of the named parameter is not as expected.
Expected:
/c/noticias/test title
What you really had to leave:
/c/test title? urlMenu=news
Obs.: In my Routeconfig I have route attribute mapping
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "TesteRotas.Controllers"}
);
What exactly do I do to achieve the result of named routes?
I still can’t see what the output shows you but, there’s already a typo here
new { urlConteudo= "titulo-teste, urlMenu = "noticias" }
the correct would benew { urlConteudo= "titulo-teste", urlMenu = "noticias" }
– novic
It really was a typo @Virgilionovic but in case I can’t really create the url by Helper the query persists
– Guilherme Almeida
I discovered the problem, it was a conflict of course. In this case the most specific route was getting last and ASP.NET always stops on the first route that suits. [Route("{urlConteudo}", Order = 1)] In the Index with only one parameter I added the value of Order so that it was after the other in the route table and solved (:
– Guilherme Almeida