Creating routes (controller/id/action) in c#

Asked

Viewed 158 times

0

I’m trying to create a very specific route on a page of my project. As usual, routes on . NET are controller/action/id but on a specific page I would like it to be controller/id/action.

What I got was:

routes.MapRoute(
    name: "Produto",
    url: "Produto/{name}/{action}",
    defaults: new { controller = "Produto", action = "Index" },
    constraints: new { name = @"^[a-z\.]{3,20}$" }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    constraints: new { controller = @"^((?!Perfil).*)" }
);

So the /Product/Car/Ver url works perfectly. The /Campaign/Edit/1 url also works perfectly.

However, if the url /Product/Car (NO END BAR) is called is not working. It is not going to the Index action. I tried to use the code below, but it didn’t work.

routes.MapRoute(
    name: "ProdutoIndex",
    url: "Produto/{name}",
    defaults: new { controller = "Produto", action = "Index" },
    constraints: new { name = @"^[a-z\.]{3,20}$" }
);

I would like it to work without the bar at the end, because forcing the user to put this bar is very bad.

  • Why do you want to do this?

  • It was requested by the client that the url be like this. The question of not having a bar at the end when the url is /Produto/{name} is a whim of mine. And also, I think if the user is going to write the url in hand, he will not put the bar at the end, so taking a 404 in the face.

  • you tried to put the two routes.?

  • yes. I put all routes. in the order: 1 - name: "Produtoindex", 2 - name: "Product", 3 - name: "Default".

No answers

Browser other questions tagged

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