Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core Multi-Language Routing

I want to do a simple multi-language web app with routes.

What I want to do is in the url: en/products and it/prodotti with the same action.

I have created two differend routes with same action/controller:

routes.MapRoute(
    name: "english",
    template: "en/products",
    defaults: new { controller = "home", action = "products" }
);
routes.MapRoute(
    name: "italiano",
    template: "it/prodotti",
    defaults: new { controller = "home", action = "products" }
);

When I want to call one of theese routes I use:

<a href="@Url.RouteUrl("english", new { Action = "home", Controller = "products"})">Products</a>
<a href="@Url.RouteUrl("italiano", new { Action = "home", Controller = "products"})">Prodotti</a>

And I get url en/products or it/prodotti

The problem is, when I want to make a form post, I cannot control which url is going to return.

<form asp-action="products" method="post">

This only returns the first value of my routes.

The question is, how can I control my routes when I sending a form like I'm doing with Url.RouteUrl link?

I tried this but this also not works:

[Route("it/prodotti")]
[Route("en/products")]
public ViewResutl products() {
    ...
    return View(new viewmodel...);
}
like image 256
Baran Tutal Avatar asked May 10 '17 19:05

Baran Tutal


1 Answers

Great news, finally i have found the answer.

    @using (Html.BeginRouteForm("routenamehere"))
    {
    <form>
    ...
    ...
    </form>
    }
like image 199
Baran Tutal Avatar answered Oct 13 '22 01:10

Baran Tutal