Create URL with .NET MVC Resource Files?

0

Basically in my RouteConfig.cs I am trying to map a route that varies depending on the current browser language.

What I'm trying to get to is a dynamic path like:

/books-with-description/
/libros-con-descripcion/

these strings are constant (they are not stored in db) and they are only two (Spanish and English)

Try with:

routes.MapRoute(
            name: "Libros",
            url:  "{index}/index/{lang}",
            defaults: new { controller = "Libros", action = "Get", lang = UrlParameter.Optional, index = @Rurl.LibrosConDescripcion }
        );

And I also tried:

routes.MapRoute(
            name: "Libros",
            url: @Rurl.LibrosConDescripcion + "/index/{lang}",
            defaults: new { controller = "Libros", action = "Get", lang = UrlParameter.Optional}
        );

Being @ Rurl.LibrosConDescripcion the access to the string in the resource file

For some reason in this case I'm not taking the language change that I set in CurrentThread.CurrentCulture. I guess it's because the application starts running in its own Thread and starts only once, regardless of the Threads created by each request from the client's browser (Correct me if I'm wrong).

Suggestions? Do not I have another option to generate that part of the URL from an ActionLink in Razor?

(The idea was to set it only once and not have to do it for every view that wants to access this url)

    
asked by Leandro hereñu 23.02.2018 в 15:55
source

1 answer

0

for the moment I managed to solve it this way:

@Html.ActionLink("Traer Libros", "Libros", "GetLibros", new { index = @Rurl.LibrosConDescripcion }, new { })

Basically this generates me a "< a >" tag in HTML in my view and since it is in the corresponding Thread if it changes with respect to culture.

My RouteConfig was like this:

routes.MapRoute(
        name: "Libros",
        url:  "{index}/index/{lang}",
        defaults: new { controller = "Libros", action = "GetLibros", lang = UrlParameter.Optional, index = UrlParameter.Optional }
    );

With the problem that I am now with, since this link is in a general menu for several pages, update the URL that appears in the browser when I am already inside the Link mentioned, which I think to do with javascript.

Otherwise, this could be a solution.

    
answered by 23.02.2018 / 17:19
source