Parameters named in - Asp.net url

Asked

Viewed 305 times

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 be new { urlConteudo= "titulo-teste", urlMenu = "noticias" }

  • It really was a typo @Virgilionovic but in case I can’t really create the url by Helper the query persists

  • 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 (:

1 answer

-1

I will use the answer field because I cannot comment yet.

What you’re doing with the snippet is sending two attributes through the URL, you can:

1-use attributes this way (pull URL information to a variable):

Request.QueryString["atributo"];

2 - update your routeconfig.Cs, try:

  new { area ="", controller = "Home", action = "Index" },
  new { area = "^$", controller = "Home", action = @"(Pagina1|Pagina2|etc..)"}
  new string[] { Home.Controllers }
  • In this case it wouldn’t be quite that, because I would have to have a single action to determine and I couldn’t use the Asp.Net Url Helper to generate the urls.

Browser other questions tagged

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