Difficulties writing a friendly url (global) in web config

4

I asked the same question in the English version but I did not succeed!

I have an application that will be translated into three languages pt / en / es. So far I'm going to use GlobalResources, I have no problems with this, but I'm having a lot of trouble trying to write a url that looks like this:

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 could not work with this url. I can even put everything I tried here, but I do not know if it will help, if you need to send me the comments that change the issue.

ps: 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>
    
asked by anonymous 18.07.2014 / 16:09

1 answer

5

One possibility is to implement an HTTP Module that will intercept all requests, and decide which page to execute. Here is an example of 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): link

    
18.07.2014 / 16:56