Difficulties writing a user friendly (global) web config url

Asked

Viewed 1,537 times

4

I asked that same question in the English version but I was unsuccessful!

I have an application that will be translated into three languages en/en/es. Until then I’ll use Globalresources, I have no problems with it, but I’m having a lot of difficulties when trying to write a url with the following appearance:

Eu tenho isso     => http://www.teste.com.br/algumacoisa
Mas preciso disso => http://www.teste.com.br/pt/algumacoisa

I know it sounds simple, but I couldn’t work with that url. I can even put everything I tried here, but I don’t know if it will help, if you need send in the comments I change the question.

ps: the urls are in the app’s web.config. And I’m in Web Forms

UPDATE 1

 <rewrite>
  <rules>
    <!-- Produtos -->
    <rule name="produtos">
      <match url="^produtos/([0-9]+)/([a-zA-Z0-9_-]+)/?$" ignoreCase="true"/>
      <action type="Rewrite" url="/produtos/Detail.aspx?id={R:1}" appendQueryString="false"/>
    </rule>
    <!-- /Produtos -->
  </rules>
</rewrite>
  • In ASP.NET, language determination is not done, in general, in the URL, but in web.config, or it is detected automatically by the browser. For example, your website would always have the http://www.teste.com.br/algumacoisa language independent URL. If it were pt or en, the URL would remain the same. What would be different would be the configuration on web.config. To achieve this, you use the internationalization mechanism of .NET. If you still want to generate Urls with the language, then you will need to use rewrite URL: http://weblogs.asp.net/scottgu/tip-trick-url-rewriting-with-asp-net

  • Is the idea would be to recover this pt/en and such, through query string as I do in other url’s, which I posted for example there...

  • Why not use the internationalisation mechanism of ASP.NET?

  • @Eduardofernandes you’re saying about Globalresources ?

  • You can also use local Resources. See the following link with an interesting explanation: http://www.codeproject.com/Articles/334820/Using-Globalization-and-Localization-in-ASP-NET

1 answer

5


One possibility is the implementation of a HTTP Module which will intercept all requests, and decide which page will be executed. Follow an example according to your need:

public class LocaleParser : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += context_BeginRequest;
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        var req = HttpContext.Current.Request.Url.AbsoluteUri;
        var targetUrl = req;

        if (req.IndexOf('/') != -1)
        {
            var langparm = req.Split('/')[1].ToLower();

            switch (langparm)
            {
                case "pt":
                    HttpContext.Current.Items["locale"] = "PT";
                    targetUrl = req.Substring(3);
                    break;
                case "en":
                    HttpContext.Current.Items["locale"] = "EN";
                    targetUrl = req.Substring(3);
                    break;
                case "es":
                    HttpContext.Current.Items["locale"] = "ES";
                    targetUrl = req.Substring(3);
                    break;
            }
        }

        HttpContext.Current.Server.Transfer(targetUrl);
    }

    public void Dispose() { }
}

More information (in English): http://msdn.microsoft.com/en-us/library/vstudio/ms227673(v=vs.100). aspx

  • 1

    Guy did a test statically here, it worked perfectly! Thank you very much! + 1 and correct answer, too bad it has no more award! uaheuahuea :D Valeuu even!

  • 1

    @Brunocasali Good that it worked for you! In case of any doubt, feel free to ask.

Browser other questions tagged

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